次の方法で共有


How to get locations and addresses

Convert addresses to geographic locations (geocoding) and convert geographic locations to addresses (reverse geocoding) by calling the methods of the MapLocationFinder class in the Windows.Services.Maps namespace.

Here's how the classes for geocoding and reverse geocoding are related.

Important  

Your app has to be authenticated before it can use many features of the Map control and map services. For more info, see How to authenticate your Maps app.

 

Before you call the methods of the MapLocationFinder class

At the top of your class file, add a using statement for the Windows.Services.Maps namespace for the MapLocationFinder class and the related classes. Typically you also have to have a using statement for the Windows.Devices.Geolocation namespace.

using Windows.Services.Maps;
using Windows.Devices.Geolocation;

Provide the token required to authenticate for Maps services by setting the ServiceToken property of the static MapService class. For more info, see How to authenticate your Maps app.

    MapService.ServiceToken = "abcdef-abcdefghijklmno";

The examples in this topic assume that the XAML page contains a TextBlock control named tbOutputText to display the results.

Converting an address or a place name to a geographic location (geocoding)

Convert an address or a place name to a geographic location (geocoding) by calling one of the overloads of the FindLocationsAsync method of the MapLocationFinder class.

The FindLocationsAsync method returns a MapLocationFinderResult object that contains a collection of matching MapLocation objects. Access this collection through the Locations property of the MapLocationFinderResult.

        private async void Geocode()
        {
            // Address or business to geocode.
            string addressToGeocode = "Microsoft";

            // 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:

Converting a geographic location to an address (reverse geocoding)

Convert a geographic location to an address (reverse geocoding) by calling the FindLocationsAtAsync method of the MapLocationFinder class.

The FindLocationsAtAsync method returns a MapLocationFinderResult object that contains a collection of matching MapLocation objects. Access this collection through the Locations property of the MapLocationFinderResult. Each MapLocation object contains a MapAddress object. Access this object through the Address property of each MapLocation.

        private async void ReverseGeocode()
        {
            // 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:

How to display pushpins, shapes, and controls on a map

How to get routes and directions

How to display maps in the Map control

How to authenticate a Maps app