Szerkesztés

Megosztás a következőn keresztül:


Perform geocoding and reverse geocoding

Important

Bing Maps for Enterprise service retirement

The UWP MapControl and map services from the Windows.Services.Maps namespace rely on Bing Maps. Bing Maps for Enterprise is deprecated and will be retired, at which point the MapControl and services will no longer receive data.

For more information, see the Bing Maps Developer Center and Bing Maps documentation.

Note

MapControl and map services require a maps authentication key called a MapServiceToken. For more info about getting and setting a maps authentication key, see Request a maps authentication key.

This guide shows you how to convert street addresses to geographic locations (geocoding) and convert geographic locations to street addresses (reverse geocoding) by calling the methods of the MapLocationFinder class in the Windows.Services.Maps namespace.

The classes involved in geocoding and reverse geocoding are organized as follows.

Important

 You must specify a maps authentication key before you can use map services. For more info, see Request a maps authentication key.

Get a location (Geocode)

This section shows how to convert a street address or a place name to a geographic location (geocoding).

  1. Call one of the overloads of the FindLocationsAsync method of the MapLocationFinder class with a place name or street address.
  2. The FindLocationsAsync method returns a MapLocationFinderResult object.
  3. Use the Locations property of the MapLocationFinderResult to expose a collection MapLocation objects. There may be multiple MapLocation objects because the system may find multiple locations that correspond to the given input.
using Windows.Services.Maps;
using Windows.Devices.Geolocation;
...
private async void geocodeButton_Click(object sender, RoutedEventArgs e)
{
   // The address or business to geocode.
   string addressToGeocode = "Microsoft";

   // The nearby location to use as a query hint.
   BasicGeoposition queryHint = new BasicGeoposition();
   queryHint.Latitude = 47.643;
   queryHint.Longitude = -122.131;
   Geopoint hintPoint = new Geopoint(queryHint);

   // Geocode the specified address, using the specified reference point
   // as a query hint. Return no more than 3 results.
   MapLocationFinderResult result =
         await MapLocationFinder.FindLocationsAsync(
                           addressToGeocode,
                           hintPoint,
                           3);

   // If the query returns results, display the coordinates
   // of the first result.
   if (result.Status == MapLocationFinderStatus.Success)
   {
      tbOutputText.Text = "result = (" +
            result.Locations[0].Point.Position.Latitude.ToString() + "," +
            result.Locations[0].Point.Position.Longitude.ToString() + ")";
   }
}

This code displays the following results to the tbOutputText textbox.

result = (47.6406099647284,-122.129339994863)

Get an address (reverse geocode)

This section shows how to convert a geographic location to an address (reverse geocoding).

  1. Call the FindLocationsAtAsync method of the MapLocationFinder class.
  2. The FindLocationsAtAsync method returns a MapLocationFinderResult object that contains a collection of matching MapLocation objects.
  3. Use the Locations property of the MapLocationFinderResult to expose a collection MapLocation objects. There may be multiple MapLocation objects because the system may find multiple locations that correspond to the given input.
  4. Access MapAddress objects through the Address property of each MapLocation.
using Windows.Services.Maps;
using Windows.Devices.Geolocation;
...
private async void reverseGeocodeButton_Click(object sender, RoutedEventArgs e)
{
   // The location to reverse geocode.
   BasicGeoposition location = new BasicGeoposition();
   location.Latitude = 47.643;
   location.Longitude = -122.131;
   Geopoint pointToReverseGeocode = new Geopoint(location);

   // Reverse geocode the specified geographic location.
   MapLocationFinderResult result =
         await MapLocationFinder.FindLocationsAtAsync(pointToReverseGeocode);

   // If the query returns results, display the name of the town
   // contained in the address of the first result.
   if (result.Status == MapLocationFinderStatus.Success)
   {
      tbOutputText.Text = "town = " +
            result.Locations[0].Address.Town;
   }
}

This code displays the following results to the tbOutputText textbox.

town = Redmond