I developed a navigation application for windows phone 7.1. It was running fine there. After updating to 8.0 my GeoCoordinateWatcher isnt working anymore. I know that I could use Geolocator instead, but I refuse to do so, because of lack of time.
For my app I read the current position of my watcher to store it for an object instance with location information. When I save my object instance the longitude and latitude are 0.0. Even when I change the position in my emulator still 0.0. The same issue occurs on my other pages, which use GeoCoordinateWatcher. It doesnt work. As I already said, on WP 7.1 - 7.8 it works very well.
public Map()
{
InitializeComponent();
watcher = new GeoCoordinateWatcher();
watcher.MovementThreshold = 20;
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
if (e.Position.Location.IsUnknown)
{
MessageBox.Show("Please wait while your prosition is determined.");
return;
}
geo.Latitude = e.Position.Location.Latitude;
geo.Longitude = e.Position.Location.Longitude;
}
try this code:
GeoCoordinateWatcher watch;
public GeoCoordinate loc = null;
public MainPage()
{
InitializeComponent();
if (watch == null)
{
watch = new GeoCoordinateWatcher(GeoPositionAccuracy.High)
{
MovementThreshold=10
};
watch.Start();
watch.PositionChanged += watch_PositionChanged;
}
}
void watch_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
//throw new NotImplementedException();
Dispatcher.BeginInvoke(()=>LocUpdate(e));
}
void LocUpdate(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
try
{
location = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
}
catch
{
MessageBox.Show("Error");
}
}
Related
I'd like to share an image in my app. However, the image is not located in a folder but it is "taken dynamically". Basically i have an Image object
Image i = new Image() { Source = await CreateBitmapFromElement(stackpanel1) };
where CreateBitmapFromElement is defined as follows
private async Task<RenderTargetBitmap> CreateBitmapFromElement(FrameworkElement uielement)
{
try
{
var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(uielement);
return renderTargetBitmap;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
return null;
}
The Windows Phone Share Contract allows to share images located in the Picture Library (for example), but what should i use in this case?
protected override void OnNavigatedTo(NavigationEventArgs e)
{
DataTransferManager.GetForCurrentView().DataRequested += OnShareDataRequested;
base.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
DataTransferManager.GetForCurrentView().DataRequested -= OnShareDataRequested;
base.OnNavigatedFrom(e);
}
private void OnShareDataRequested(DataTransferManager sender, DataRequestedEventArgs _dataRequestedEventArgs)
{
DataRequest request = _dataRequestedEventArgs.Request;
request.Data.Properties.Title = "KeyTreat Sticker";
request.Data.Properties.Description = "KeyTreat Sticker: " + StickerName;
// Because we are making async calls in the DataRequested event handler,
// we need to get the deferral first.
DataRequestDeferral deferral = request.GetDeferral();
// Make sure we always call Complete on the deferral.
try
{
request.Data.SetStorageItems(storageItemsObject);
request.Data.Properties.Thumbnail = RandomAccessStreamReference.CreateFromFile(StorageFileObject);
request.Data.SetBitmap(RandomAccessStreamReference.CreateFromFile(StorageFileObject));
}
finally
{
deferral.Complete();
}
}
I have the below code which works fine except that it gives the last location details it tracked when application is restarted.
And it gives the correct location details after I click on btnGetLocationDetails button 2 or 3 times.
Any idea how I can fix this issue so that every time app is launched user doesn't need to click on this button 2 or 3 times to get the correct current location?
Code:
public partial class MainPage : PhoneApplicationPage
{
GeoCoordinateWatcher watcher = null;
Location location=null;
private void btnGetLocationDetails_Click_1(object sender, RoutedEventArgs e)
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
if (watcher.Permission == GeoPositionPermission.Granted)
{
watcher.MovementThreshold = Convert.ToDouble("100");
}
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>(watcher_PositionChanged);
// PositionChanged events occur whenever your position changes
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_OnStatusChanged);
watcher.Start();
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
//get the coordinates
location = new Location();
location.Latitude = e.Position.Location.Latitude;
location.Longitude = e.Position.Location.Longitude;
location.Altitude = e.Position.Location.Altitude;
}
void watcher_OnStatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
if (e.Status == GeoPositionStatus.Disabled)
MessageBox.Show("The location service is turned off.");
else if (e.Status == GeoPositionStatus.NoData)
MessageBox.Show("No location data is available. ");
}
}
I'm using Geocode Service in wp7 to get CountryName from Latitude and Longitude
I wrote following things : -
GeoCoordinate location1 = null;
private GeoCoordinateWatcher watcher;
public void FindCountryUsingGps()
{
progressbar.Visibility = Visibility.Visible;
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
watcher.Start();
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
if (watcher != null)
{
watcher.Stop();
watcher.Dispose();
watcher = null;
location1 = e.Position.Location;
ObtainCountry();//by this function you obtain country region
}
}
then added following reference :-
after that
I wrote
public void ObtainCountry()
{
if (location == null)
return;
ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();
but i'm not able to write this
ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();
line , it's comes as red
in wp7 , what's this?
Try removing/re-adding the service reference. Sometimes I've had issues where the code didn't get generated properly. The code should be under the ServiceReference folder, in a code file called Reference.cs.
Trying to test my WP7 app that uses location following this tutorial.
I have additional tools open, start the emulator from VS, let the app launch and then I place a pin in Live-mode in the Additional Tools Location utility, but no event is fired.
Is there anything wrong with my code?
public MainPage()
{
InitializeComponent();
InitWatcher();
}
private void InitWatcher()
{
geoWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
geoWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(geoWatcher_PositionChanged);
geoWatcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(geoWatcher_StatusChanged);
}
private void geoWatcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
var lol = e;
}
private void geoWatcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
var FK = e;
}
The problem is that you have to start the GeoCoordinateWatcher:
geoWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
geoWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(geoWatcher_PositionChanged);
geoWatcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(geoWatcher_StatusChanged);
geoWatcher.Start();
Do you need to call the Start method on your GeoCoordinateWatcher instance?
http://msdn.microsoft.com/en-us/library/ee808853.aspx
I'm using this to make a web request and download some data:
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
var client = new WebClient();
client.DownloadStringCompleted += (s, e) => {
textBlock1.Text = e.Result;
};
client.DownloadStringAsync(new Uri("http://example.com"));
}
}
The text of textBlock1 never changes even though e.Result has the correct data. How do I update that from the callback?
Edit: If I add MessageBox.Show(e.Result); in the callback along with the textBlock1.Text assignment, both the messsage box and the text box show the correct data.
Edit Again: If I add a TextBox and set it's text right after the line textBlock1.Text line, they both show the correct text.
I think, it's a bug.
I also ran into some problems with updating the UI from different dispatchers. What I finally did was use the TextBlock's (or other UI Element) own dispatcher and that worked for me. I think the phone framework may be using different dispatchers between the app and UI Elements. Notice the change from dispatcher.BeginInvoke to textbox1.Dispatcher...
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var dispatcher = Deployment.Current.Dispatcher;
var client = new WebClient();
client.DownloadStringCompleted += (s, e) =>
{
var result = e.Result;
textBlock1.Dispatcher.BeginInvoke(
()=> textBlock1.Text = result
);
};
client.DownloadStringAsync(new Uri("http://example.com"));
}
From browsing through the WP7 forums, a bunch of people were reporting that this was related to a video card driver issue. I've updated my ATI Radeon HD 3400 drivers to the latest version and it appears to work now.
client.DownloadStringAsync is expecting a Uri like this:
client.DownloadStringAsync(new Uri("http://example.com"));
also, shouldn't you update your TextBlock through a Dispatcher.BeginInvoke like this:
client.DownloadStringCompleted += (s, e) =>
{
if (null == e.Error)
Dispatcher.BeginInvoke(() => UpdateStatus(e.Result));
else
Dispatcher.BeginInvoke(() => UpdateStatus("Operation failed: " + e.Error.Message));
};
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
Loaded += MainPage_Loaded;
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var dispatcher = Deployment.Current.Dispatcher;
var client = new WebClient();
client.DownloadStringCompleted += (s, e) =>
{
var result = e.Result;
dispatcher.BeginInvoke(
()=> textBlock1.Text = result
);
};
client.DownloadStringAsync(new Uri("http://example.com"));
}
}
I want to comment but can't yet. Yes, I have a very similar issue. In my case it's my viewmodel that is updating a DownloadStatus property, then when the download is completed I do some more work and continue updating this property.
The view stops updating once the ViewModel code hits the OpenReadCompleted method. I've stepped carefully through the code. PropertyChanged fires, and the view even comes back and retrieves the new property value, but never shows the change.
I was sure it was a bug, but then I created a brand new project to reproduce the issue, and it works fine!
Here's a snippet of my non-reproducing code. The UI textblock bound to "DownloadStatus" happily updates properly all the way through. But the same paradigm doesn't work in my main project. Infuriating!
public void BeginDownload(bool doWorkAfterDownload)
{
DownloadStatus = "Starting ...";
_doExtraWork = doWorkAfterDownload;
var webClient = new WebClient();
string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("test:password"));
webClient.Headers["Authorization"] = auth;
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri("http://www.ben.geek.nz/samsung1.jpg"));
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error != null)
{
DownloadStatus = e.Error.Message;
return;
}
DownloadStatus = "Completed. Idle.";
if(_doExtraWork)
{
Thread t = new Thread(DoWork);
t.Start(e.Result);
}
}
void DoWork(object param)
{
InvokeDownloadCompleted(new EventArgs());
// just do some updating
for (int i = 1; i <= 10; i++)
{
DownloadStatus = string.Format("Doing work {0}/10", i);
Thread.Sleep(500);
}
DownloadStatus = "Completed extra work. Idle.";
InvokeExtraWorkCompleted(new EventArgs());
}