首页 文章

如何用 Map 反转地理编码?

提问于
浏览
-1

我正在使用 leaflet 在 Map 上显示标记,当我在 markerclick 时,我得到 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);
        }
      });
    }

但它给了我:

无法读取未定义的属性'geocode'

NOTE

这条线很好

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

好像我做console.log我得到了正确的 latlng

2 回答

  • 1

    这可能是因为google api尚未加载,您可以尝试在其他脚本之前加载它,以确保检查 console.log("google api object is", geocoder) 并检查Geocode以验证google是否已加载,然后再调用api . 编辑:你没有通过它 . 因为当变量名相同时,参数将优先于外部作用域 .

    以下程序将为您提供用户当前位置的地址,您可以传递任何lat,lng并获取其地址: -

    //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!");
            }
    
  • 0

    您的代码中有拼写错误 . 您没有将对地理编码器的引用传递给 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
      });
    }
    

相关问题