/ / Reverse Geocode auf Client-Seite (Google Maps V3 API) - Javascript, Google-Maps, Geocoding, Reverse-Geocoding

Reverse Geocode auf Client-Seite (Google Maps V3 API) - Javascript, Google-Maps, Geocodierung, Reverse-Geocodierung

Wie führen Sie einen Reverse-Geocode auf der Clientseite mithilfe der Google Maps V3-API durch? Der Vorwärtsgeocode von Adresse zu LatLng ist unkompliziert (Code unten), aber wie machen Sie das Gleiche für Reverse-Geocode?

Normaler Geocode-Code:

geocoder = new google.maps.Geocoder();
geocoder.geocode( { "address": address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});

Antworten:

13 für die Antwort № 1

Der Prozess ist genau derselbe, mit dem geringfügigen Unterschied, dass Sie der Geocodefunktion nicht ein Adressobjekt angeben, sondern ein LatLng-Objekt

Reverse Geocode Code:

var input = document.getElementById("latlng").value;
var latlngStr = input.split(",",2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);

geocoder.geocode({"latLng": latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
map.setZoom(11);
marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent(results[1].formatted_address);
infowindow.open(map, marker);
} else {
alert("No results found");
}
} else {
alert("Geocoder failed due to: " + status);
}
});

Beispiel direkt von Google

Ich hoffe, das hilft.