Starting with Windows Phone 8 Location Services

Today I looked at the location services for Windows Phone 8.

In this blog post we will look at these three things:
1. Get decice location
2. Get street/city or whatever by coordinates (Geocode)
3. Get coordinates by street or city (ReverseGeocode)

Full sourcecode is also hosted on pastebin

To get the device location, the app needs the “ID_CAP_LOCATION” capability.
Here is the code for getting the device location.

         /// <summary>
        /// Get actual Position from GPS/Wifi/gsm
        /// </summary>
        /// <returns></returns>
        private async Task<Geoposition> getLocation(int maxAgeInMinutes = 5, int timeOutinSeconds = 10)
            Geolocator geolocator = new Geolocator();

            Geoposition geoposition = null;

            if (geolocator.LocationStatus != PositionStatus.Disabled) // Check if we can access the LocationService
                geoposition = await geolocator.GetGeopositionAsync(
                maximumAge: TimeSpan.FromMinutes(maxAgeInMinutes),
                timeout: TimeSpan.FromSeconds(timeOutinSeconds)

            return geoposition;

As result you will get something like this:

A simple thing is to get the full address of a location described trough latitude/longitude.
In Windows Phone 7.x the developer had to create a bing maps developer access token. With this token you can query the bing maps rest service and get the result.
I am glad that with Windows Phone 8, there is a build in library for most of the Geocode tasks (I think internally this library is doing the same thing I described above Smile).

         /// <summary>
        /// Get Location name by lat and long
        /// </summary>
        private async void getLocationName()
            Geoposition geoposition = await getLocation(1);

            List<MapLocation> locations;
            ReverseGeocodeQuery query = new ReverseGeocodeQuery();
            query.GeoCoordinate = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
            query.QueryCompleted += (s, e) =>
                if (e.Error == null && e.Result.Count > 0)
                    locations = e.Result as List<MapLocation>;

                    Model.LocationName = locations[0].Information.Address.Street;
                    // Do whatever you want with returned locations. 
                    // e.g. MapAddress address = locations[0].Information.Address;

The next thing I looked at is called ReverseGeocode. You have the name of a street or a city and you need the longitude and the latide for this location. For example for showing it on a map.
You can achieve this, with this code:

/// <summary>
        /// Get Coordinates from adress
        /// </summary>
        /// <param name="searchTerm"></param>
        private async void getLocationCoordsfromAdress(string searchTerm)
            GeocodeQuery a = new GeocodeQuery();

            Geoposition geoposition = await getLocation();

            if (geoposition != null)
                a.GeoCoordinate = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
                a.GeoCoordinate = new GeoCoordinate();
            a.SearchTerm = searchTerm; // mock search, replace with search value

            a.QueryCompleted += (s, e) =>
                if (e.Error == null && e.Result.Count > 0)
                    // DO SOMETHING SERIOUS



With this three operations you have good start in the world of Windows Phone 8 location services.

Posted Jan 19 2015, 05:16 PM by Holger Vetter
Filed under: is a .Net Community Blog powered by daenet GmbH.