PositionChanged event gives the old location details when application is restarted - windows-phone-7

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. ");
}
}

Related

GeoCoordinateWatcher isn't working after migration to WP8

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");
}
}

PositionChanged & StatusChanged event getting fired twice in WP7

I am trying to getting coordinates with the help of GPS and when i am putting a dry run or debug the event makes PositionChange & StatusChange to call TWICE. Here is my code please help me.
private void button1_Click(object sender, RoutedEventArgs e)
{
if (NetworkInterface.GetIsNetworkAvailable())
{
flag = true;
if (watcher == null)
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
watcher.MovementThreshold = 20;
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.Start();
}
}
}
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
MessageBox.Show("Location Service is not enabled on the device");
break;
case GeoPositionStatus.NoData:
MessageBox.Show(" The Location Service is working, but it cannot get location data.");
break;
}
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
if (e.Position.Location.IsUnknown)
{
MessageBox.Show("Please wait while your prosition is determined....");
return;
}
List<string> locationData = new List<string>();
locationData.Add(e.Position.Location.Latitude.ToString("Latitude:" + "0.000"));
locationData.Add(e.Position.Location.Longitude.ToString("Longitude:" + "0.000"));
locationData.Add(e.Position.Location.Altitude.ToString());
locationData.Add(e.Position.Location.Speed.ToString());
locationData.Add(e.Position.Location.Course.ToString());
}
It's called twice because the status changes from
Initializing -> Ready
For Initializing it fires once and For ready the second time :)

How can I use Bing Maps to get my current location?

My code is not working, it's show the whole world map. I just want to get current location and place a pushpin at it. I have been through Google and none of the examples works.
This is my code.
GeoCoordinateWatcher watcher;
// Constructor
public MainPage()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
if (watcher == null)
{
Console.WriteLine(watcher);
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
watcher.MovementThreshold = 20;
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
}
watcher.Start();
}
private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
Pushpin pin = new Pushpin();
pin.Template = this.Resources["currentPushPin"] as ControlTemplate;
pin.Location = watcher.Position.Location;
mapControl.Items.Add(pin);
myMap.SetView(watcher.Position.Location, 15.0);
watcher.Stop();
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
if (e.Position.Location.IsUnknown)
{
MessageBox.Show("Please wait while your prosition is determined....");
return;
}
Pushpin pin = new Pushpin();
myMap.Center = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
pin.Template = this.Resources["currentPushPin"] as ControlTemplate;
pin.Location = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
myMap.SetView(watcher.Position.Location, 18.0);
}
You start the GeoCordinateWatcher in PhoneApplicationPage_Loaded but this method never gets called. Add
Loaded+=PhoneApplicationPage_Loaded
at the end of the constructor.

Where to access the isolated storage value?

public IsolatedStorageSettings appSettings =
IsolatedStorageSettings.ApplicationSettings;
public Settings()
{
InitializeComponent();
this.toggle.Checked += new EventHandler<RoutedEventArgs>(toggle_Checked);
this.toggle.Unchecked += new EventHandler<RoutedEventArgs>(toggle_Unchecked);
this.toggle.Click += new EventHandler<RoutedEventArgs>(toggle_Click);
this.toggle.Indeterminate += new EventHandler<RoutedEventArgs>(toggle_Indeterminate);
`}`
void toggle_Unchecked(object sender, RoutedEventArgs e)
{
this.toggle.Content = "Visibity is off";
this.toggle.SwitchForeground = new SolidColorBrush(Colors.Red);
appSettings.Add("value", "off");
}
void toggle_Checked(object sender, RoutedEventArgs e)
{
this.toggle.Content = "Visibity is on";
this.toggle.SwitchForeground = new SolidColorBrush(Colors.Green);
appSettings.Add("value", "on");
}
void toggle_Indeterminate(object sender, RoutedEventArgs e)
{
//add some content here
}
void toggle_Click(object sender, RoutedEventArgs e)
{
//add some content here
}
by default calling checked method.If a user unchcked the button then again an user logged in app need to show the unchcked because the user previously unchcked the btn.but it's show checked.for that i am saving one value in isolated storage.
can you please tell me where to access the isolated varieable value ?
You can access the isolatedstorage value in any page, in the same way as you created it.
Try to access the value in Settings() constructor after the InitializeComponent();
public Settings()
{
InitializeComponent();
string value;
if (appSettings.Contains("value"))
{
appSettings.TryGetValue("value", out value);
}
and then you can change the value of toggle button based on the 'value'.
It seems that you are not calling the Save method on the ApplicationSettings object.
Please read this guide on how you should work with isolated storage.
To save a setting:
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (!settings.Contains("userData"))
{
settings.Add("userData", "some value");
}
else
{
settings["userData"] = "some value";
}
settings.Save();
To retrieve a setting:
if (IsolatedStorageSettings.ApplicationSettings.Contains("userData"))
{
string result IsolatedStorageSettings.ApplicationSettings["userData"] as string;
}
So in your case, save the state of the CheckBox in the Checked & UnChecked event, and load the state in the init of the page.

WP7 location tools doesn't fire positionChanged event

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

Resources