【问题标题】:How to reverse geocode with maps?如何使用地图反向地理编码?
【发布时间】:2018-12-09 17:25:16
【问题描述】:

我正在使用传单在地图上显示标记,当我在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);
        }
      });
    }

但它给了我:

Cannot read property 'geocode' of undefined

注意

这条线很好

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

就像我做 console.log 一样,我得到了正确的 latlng

【问题讨论】:

    标签: javascript jquery google-maps leaflet google-geocoder


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      这可能是因为google api还没有加载,您可以尝试在其他脚本之前加载它,以确保在调用api之前检查console.log("google api object is", geocoder)并检查Geocode以验证google是否已加载。 编辑:您不需要地理编码器作为 geocodeLatLng 函数中的参数,正如@geocodezip 所指出的,如果您不通过它,它将是未定义的。因为当变量名称相同时,参数将优先于外部范围。

      下面的过程会给你用户当前位置的地址,你可以通过任意的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!");
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-17
        • 2015-07-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多