Search code examples
c#windows-phone-8reverse-geocodingservice-reference

Windows Phone ReverseGeocoding to get Address from Lat and Long


i am using the following service reference to get location details from latitude and longnitude

http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc

i added the above URL to my Service reference class and try to get the location details by calling the below method

 public void reverse()
       {
           string Results = "";
           try
           {
               // Set a Bing Maps key before making a request
               string key = "Bing Maps Key";

               ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();

               // Set the credentials using a valid Bing Maps key
               reverseGeocodeRequest.Credentials = new GeoCodeService.Credentials();
               reverseGeocodeRequest.Credentials.ApplicationId = key;

               // Set the point to use to find a matching address
               GeoCodeService.Location point = new GeoCodeService.Location();
               point.Latitude = 47.608;
               point.Longitude = -122.337;

               reverseGeocodeRequest.Location = point;

               // Make the reverse geocode request
               GeocodeServiceClient geocodeService = new GeocodeServiceClient();
               **GeocodeResponse geocodeResponse = geocodeService.ReverseGeocode(reverseGeocodeRequest);**



           }
           catch (Exception ex)
           {
               Results = "An exception occurred: " + ex.Message;

           }

but i am getting the following error message

  1. GeoCodeService.GeoCodeServiceClient does not contain a definition for ReverseGeocode and no extension method

  2. GeoCodeService.GeoCodeServiceClient could not be found.

help me in solving the problem.and also tell me is this a best way to find location details .


Solution

  • In Windows Phone 8 you have built-in API for reverse geocoding, without the need of adding a Service Reference to Bing Maps:

            List<MapLocation> locations;
            ReverseGeocodeQuery query = new ReverseGeocodeQuery();
            query.GeoCoordinate = new GeoCoordinate(47.608, -122.337);
            query.QueryCompleted += (s, e) =>
                {
                    if (e.Error == null && e.Result.Count > 0)
                    {
                        locations = e.Result as List<MapLocation>;
                        // Do whatever you want with returned locations. 
                        // e.g. MapAddress address = locations[0].Information.Address;
                    }
                };
            query.QueryAsync();