title | description | ms.assetid | ms.date | ms.topic | keywords | ms.localizationpriority |
---|---|---|---|---|---|---|
Perform geocoding and reverse geocoding |
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. |
B912BE80-3E1D-43BB-918F-7A43327597D2 |
06/21/2024 |
article |
windows 10, uwp, geocoding, map, location |
medium |
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.
- The MapLocationFinder class contains methods that handle geocoding (FindLocationsAsync) and reverse geocoding (FindLocationsAtAsync).
- These methods both return a MapLocationFinderResult instance.
- The Locations property of the MapLocationFinderResult exposes a collection of MapLocation objects.
- MapLocation objects have both an Address property, which exposes a MapAddress object representing a street address, and a Point property, which exposes a Geopoint object representing a geographic location.
Important
You must specify a maps authentication key before you can use map services. For more info, see Request a maps authentication key.
This section shows how to convert a street address or a place name to a geographic location (geocoding).
- Call one of the overloads of the FindLocationsAsync method of the MapLocationFinder class with a place name or street address.
- The FindLocationsAsync method returns a MapLocationFinderResult object.
- 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)
This section shows how to convert a geographic location to an address (reverse geocoding).
- Call the FindLocationsAtAsync method of the MapLocationFinder class.
- The FindLocationsAtAsync method returns a MapLocationFinderResult object that contains a collection of matching MapLocation objects.
- 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.
- 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