3

I've just tried my first WP7 application on a real device. My problem is that I use geolocation for one of the features, but I always get location unknown. I don't know if there's any way to grant location permissions to my app or if I am missing something. In the phone settings the location services are enabled, and maps app is working without any problem in finding my actual position. I've checked the GeoCoordinateWatcher.Permisson property and its value is "Granted". I already have this line <Capability Name="ID_CAP_LOCATION"/> in WMAppManifest.xml.

Any ideas to solve it?

[EDIT]

Here's my code. I've added the start line after you told me to do so, but I'm still having te problem.

 string location = "41,0";
 GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
 watcher.Start();
 var myPosition = watcher.Position;

 if (!myPosition.Location.IsUnknown) {
 location = myPosition.Location.Latitude + "," +  myPosition.Location.Longitude;

}

enkara
  • 6,189
  • 6
  • 34
  • 52
  • Can you provide the code that you are using to obtain location information? Also what does your WMAppManifest.xml look like? – ColinE May 23 '11 at 15:57
  • Did you call Start() on the GeoCoordinateWatcher instance you're using? You can also check [Jaime Rodriguez's tips](http://blogs.msdn.com/b/jaimer/archive/2010/11/11/geocoordinatewatcher-tips-part1.aspx) to see if that rings a bell. – Andréas Saudemont May 23 '11 at 16:59
  • I've edited the post to show you my code. I've based it in this tutorial: http://channel9.msdn.com/Series/Windows-Phone-7-Development-for-Absolute-Beginners/GPS-Location-API-and-Calling-Web-Services My manifest is the default one. – enkara May 25 '11 at 09:11

1 Answers1

3

Wait for location services to be ready. Your GeoCoordinateWatcher has an event for status change and another one for position change. Your code should look like this.

//this goes somewhere in your startup sequence
_geoCoordinateWatcher.StatusChanged += 
  new EventHandler<GeoPositionStatusChangedEventArgs>(_gcw_StatusChanged);
_geoCoordinateWatcher.PositionChanged += 
  new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(_gcw_PositionChanged);
_geoCoordinateWatcher.MovementThreshold = 50; //metres
_geoCoordinateWatcher.Start();

...

static void _gcw_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
  if (e.Status == GeoPositionStatus.Ready)
    PhoneApplicationService.Current.State["CurrentLocation"] = 
      _geoCoordinateWatcher.Position.Location;
}

static void _gcw_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
  PhoneApplicationService.Current.State["CurrentLocation"] = e.Position.Location;
}

Since you seem to be having some grief I suggest you start by putting a messagebox in the status change event so you can tell whether it fires on your phone, and once you get that sorted try for position change etc.

Also, have you tried going outside? You may not get a GPS lock inside and cell tower location doesn't always work. Go outside and get clear of tall buildings. If you live in a highrise, go out on a balcony or (best of all) up on the roof.

Peter Wone
  • 17,965
  • 12
  • 82
  • 134
  • Thank you for your help. I've been working on other projects and I had to abandon this one, but I'm working on it again. I've tried your code but as I am a novice, I don't know how to declare some varibles where visual studio is complaining. I've done a search in Google and I've come to this page: http://www.codeproject.com/Articles/134982/A-helper-class-to-get-the-current-location-on-a-Wi I'm not getting any results with this class either. I control every function and I only enter to the constructor and GetLocation. The problem is not the GPS signal, as other apps in the phone work ok – enkara Jul 13 '11 at 10:18