Location always returns "Unknown"
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;
}
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.
精彩评论