Melakukan geocoding dan geocoding terbalik

Catatan

Layanan MapControl dan peta memerlukan kunci autentikasi peta yang disebut MapServiceToken. Untuk informasi selengkapnya tentang mendapatkan dan mengatur kunci autentikasi peta, lihat Meminta kunci autentikasi peta.

Panduan ini menunjukkan kepada Anda cara mengonversi alamat jalan ke lokasi geografis (geocoding) dan mengonversi lokasi geografis ke alamat jalan (geocoding terbalik) dengan memanggil metode kelas MapLocationFinder di namespace Layanan Windows.Services.Maps .

Tip

Untuk mempelajari selengkapnya tentang menggunakan peta di aplikasi Anda, unduh sampel MapControl dari repositori sampel universal Windows di GitHub.

Kelas yang terlibat dalam geocoding dan geocoding terbalik diatur sebagai berikut.

Penting

 Anda harus menentukan kunci autentikasi peta sebelum dapat menggunakan layanan peta. Untuk informasi selengkapnya, lihat Meminta kunci autentikasi peta.

Mendapatkan lokasi (Geocode)

Bagian ini memperlihatkan cara mengonversi alamat jalan atau nama tempat ke lokasi geografis (geocoding).

  1. Panggil salah satu kelebihan beban metode FindLocationsAsync dari kelas MapLocationFinder dengan nama tempat atau alamat jalan.
  2. Metode FindLocationsAsync mengembalikan objek MapLocationFinderResult .
  3. Gunakan properti LokasimapLocationFinderResult untuk mengekspos kumpulan objek MapLocation . Mungkin ada beberapa objek MapLocation karena sistem mungkin menemukan beberapa lokasi yang sesuai dengan input yang diberikan.
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() + ")";
   }
}

Kode ini menampilkan hasil berikut ke tbOutputText kotak teks.

result = (47.6406099647284,-122.129339994863)

Mendapatkan alamat (geocode terbalik)

Bagian ini memperlihatkan cara mengonversi lokasi geografis ke alamat (geocoding terbalik).

  1. Panggil metode FindLocationsAtAsync dari kelas MapLocationFinder .
  2. Metode FindLocationsAtAsync mengembalikan objek MapLocationFinderResult yang berisi kumpulan objek MapLocation yang cocok.
  3. Gunakan properti LokasimapLocationFinderResult untuk mengekspos kumpulan objek MapLocation . Mungkin ada beberapa objek MapLocation karena sistem mungkin menemukan beberapa lokasi yang sesuai dengan input yang diberikan.
  4. Akses objek MapAddress melalui properti Alamat dari setiap 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;
   }
}

Kode ini menampilkan hasil berikut ke tbOutputText kotak teks.

town = Redmond