Domanda

I'm trying to find the towns near to the current position with geolocation services and the map services of Windows Phone 8. I've been looking on many websites but I haven't found anything for WP8 and I don't know how to do it.

Can this be done with the WP8 SDK o do I need to use a external service?

Thanks in advance

È stato utile?

Soluzione

Yes, you can get current address (towns in your case) by using ReverseGeocodeQueryclass in Microsoft.Phone.Maps.Services API. First, try to get the current coordinate like this:

Geolocator geolocator = new Geolocator();     
geolocator.DesiredAccuracyInMeters = 100; 
Geoposition geoposition = await geolocator.GetGeopositionAsync(
                         maximumAge: TimeSpan.FromMinutes(5),
                         timeout: TimeSpan.FromSeconds(5));

Then, use the coordinate to query the address:

ReverseGeocodeQuery query = new ReverseGeocodeQuery();
query.GeoCoordinate = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
query.QueryCompleted += (s, ev) =>
{
   if (ev.Error == null && ev.Result.Count > 0)
   {
       List<MapLocation> locations = ev.Result as List<MapLocation>;
       // do what you want with the locations...
   }
}
query.QueryAsync();

More detailed example can be found here.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top