PositionChanged event gives the old location details when application is restarted
-
Sunday, January 13, 2013 6:05 AM
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. ");
}
}- Edited by Krrrishna Sunday, January 13, 2013 6:06 AM
All Replies
-
Sunday, January 13, 2013 12:06 PM
In the button click event you could be causing a memory leak. You were adding a new event handlers with out clearing out the old one. I would try something like this.
if (watcher == null) { watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); if (watcher.Permission == GeoPositionPermission.Granted) { watcher.MovementThreshold = Convert.ToDouble("100"); } watcher.PositionChanged -= watcher_PositionChanged; // PositionChanged events occur whenever your position changes watcher.StatusChanged -= new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_OnStatusChanged); watcher.PositionChanged += watcher_PositionChanged; // PositionChanged events occur whenever your position changes watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_OnStatusChanged); } watcher.Start();
-
Tuesday, January 15, 2013 2:38 PM
Still the same result. The below code is how I modified.
watcher.PositionChanged -= new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged); // PositionChanged events occur whenever your position changes watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged); // PositionChanged events occur whenever your position changes watcher.StatusChanged -= new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_OnStatusChanged); watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_OnStatusChanged); watcher.Start();
-
Thursday, January 17, 2013 12:49 PMwhen I pressed the button after app restarted I got the event to fire location changed event twice once with the old location and then with the new location. it probably would be better to stop the location service when the app goes to sleep.
-
Saturday, January 19, 2013 5:09 AM
Though I could avoid the last location it cached by setting watcher.stop() watcher.dispose() and watcher = null
But still the PositionChanged event is fired two times .It's not giving the exact position where I am in a single event.
- Edited by Krrrishna Saturday, January 19, 2013 5:10 AM
-
Saturday, January 19, 2013 9:30 PM
Since Windows Phone 7 the behaviour that I have seen is when the location watcher is started it initially sends the last known location and then starts sending updated locations when they become available. The initial double-firing of the event sounds fairly consistent with this.Though I could avoid the last location it cached by setting watcher.stop() watcher.dispose() and watcher = null
But still the PositionChanged event is fired two times .It's not giving the exact position where I am in a single event.
Joel Ivory Johnson | http://www.j2i.net | Windows Phone Developer MVPIt takes all the running you can do to stay in one place.If you want to get somewhere else,you must try to run at least twice as fast as that. -
Saturday, January 19, 2013 11:12 PMIn reference to Joel's reply, this thread will be worth a shot,
http://awkwardcoder.blogspot.com/2011/09/geo-location-on-wp7-dont-trust-first.html -
Tuesday, January 29, 2013 2:01 AM
It always gives last known location when I run the same code(which double fires) in the Background agent.
Is there a way to fix this in the code of Background agent.?

