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.
- Kelas MapLocationFinder berisi metode yang menangani geocoding (FindLocationsAsync) dan geocoding terbalik (FindLocationsAtAsync).
- Metode ini mengembalikan instans MapLocationFinderResult .
- Properti LokasimapLocationFinderResult mengekspos kumpulan objek MapLocation .
- Objek MapLocation memiliki properti Alamat, yang mengekspos objek MapAddress yang mewakili alamat jalan, dan properti Titik, yang mengekspos objek Geopoint yang mewakili lokasi geografis.
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).
- Panggil salah satu kelebihan beban metode FindLocationsAsync dari kelas MapLocationFinder dengan nama tempat atau alamat jalan.
- Metode FindLocationsAsync mengembalikan objek MapLocationFinderResult .
- 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).
- Panggil metode FindLocationsAtAsync dari kelas MapLocationFinder .
- Metode FindLocationsAtAsync mengembalikan objek MapLocationFinderResult yang berisi kumpulan objek MapLocation yang cocok.
- 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.
- 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
Topik terkait
- Sampel peta UWP
- Sampel aplikasi lalu lintas UWP
- Panduan desain untuk peta
- Video: Memanfaatkan Peta dan Lokasi Di Ponsel, Tablet, dan PC di Aplikasi Windows Anda
- Pusat Pengembang Bing Maps
- Kelas MapLocationFinder
- Metode FindLocationsAsync
- Metode FindLocationsAtAsync
Saran dan Komentar
https://aka.ms/ContentUserFeedback.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat:Kirim dan lihat umpan balik untuk