簡體   English   中英

如何使用地圖反向地理編碼?

[英]How to reverse geocode with maps?

我正在使用傳單在地圖上顯示標記,當我click marker ,會得到其latlng ,然后將它們發送到google maps geocoder來檢索地址名稱:

var markerCoords = [];
circle.on('click', function (e) {
    var curPos = e.target.getLatLng();
    markerCoords.push(curPos.lng);
    markerCoords.push(curPos.lat);
    geocodeLatLng();
});

    var geocoder = new google.maps.Geocoder;
    function geocodeLatLng(geocoder) {
      var latlng = {lat: parseFloat(markerCoords[1]), lng: parseFloat(markerCoords[0])};
      geocoder.geocode({'location': latlng}, function(results, status) {
        if (status === 'OK') {
          if (results[0]) {
            console.log(results[0].formatted_address);
          } else {
            window.alert('No results found');
          }
        } else {
          window.alert('Geocoder failed due to: ' + status);
        }
      });
    }

但這給了我:

 Cannot read property 'geocode' of undefined 

注意

這條線很好

var latlng = {lat: parseFloat(markerCoords[1]), lng: parseFloat(markerCoords[0])};

就好像我做我CONSOLE.LOG得到正確的latlng

您的代碼中有錯字。 您沒有將對geocodeLatLng的引用傳遞給geocodeLatLng函數,因此在函數內部為null

var markerCoords = [];
circle.on('click', function (e) {
    var curPos = e.target.getLatLng();
    markerCoords.push(curPos.lng);
    markerCoords.push(curPos.lat);
    geocodeLatLng(geocoder);  // <============================================== **here**
});

var geocoder = new google.maps.Geocoder;
function geocodeLatLng(geocoder) { 
  var latlng = {lat: parseFloat(markerCoords[1]), lng: parseFloat(markerCoords[0])};
  geocoder.geocode({'location': latlng}, function(results, status) {
  // ... code to process the result
  });
}

這可能是因為google api尚未加載,您可以嘗試在其他腳本之前加載它,以確保檢查console.log("google api object is", geocoder)並檢查Geocode以驗證google是否已加載,然后再調用api。 編輯:您不需要geocoder作為geocodeLatLng函數中的參數,正如@geocodezip指出的那樣,如果不傳遞,則它將是未定義的。 因為當變量名稱相同時,參數將優先於外部作用域。

以下步驟將為您提供用戶當前位置的地址,您可以傳遞任意緯度,經度並獲取其地址:-

//getting location address from latitude and longitude with google api
    navigator.geolocation.getCurrentPosition(success, error);
        function success(position) {
                var lat = position.coords.latitude;
                var long = position.coords.longitude;
                var geocoder = new google.maps.Geocoder;
                console.log("google api object is", geocoder)                
                var latlng = { lat: lat, lng: long };
                geocoder.geocode({ 'location': latlng }, function (results, status) {

                    if (status === 'OK') {
                        if (results[0]) {

                       console.log(results[0].formatted_address);// this will be actual full address
                        } else {
                            alert('No results found');
                        }
                    } else {
                        alert('Geocoder failed due to: ' + status);

                    }
                });

        }


        function error(err) {
            alert("Allow location services!");
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM