Bagikan melalui


Melakukan geocoding dan geocoding terbalik

Penting

Penghentian layanan Bing Maps for Enterprise

Layanan UWP MapControl dan peta dari namespace Windows.Services.Maps mengandalkan Bing Maps. Bing Maps for Enterprise tidak digunakan lagi dan akan dihentikan, di mana MapControl dan layanan tidak akan lagi menerima data.

Untuk informasi selengkapnya, lihat dokumentasi Pusat Pengembang Bing Maps dan Bing Maps.

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 Windows.Services.Maps.

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 Lokasi dari MapLocationFinderResult untuk mengekspos objek MapLocation koleksi. 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 kotak tbOutputText teks.

result = (47.6406099647284,-122.129339994863)

Mendapatkan alamat (geocode terbalik)

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

  1. Panggil metode FindLocationsAtAsync dari kelas MapLocationFinder.
  2. Metode FindLocationsAtAsync mengembalikan objek MapLocationFinderResult yang berisi kumpulan objek MapLocation yang cocok.
  3. Gunakan properti Lokasi dari MapLocationFinderResult untuk mengekspos objek MapLocation koleksi. 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 kotak tbOutputText teks.

town = Redmond