【问题标题】:Google Maps - Cannot read properties of undefined (reading 'spherical') [closed]谷歌地图 - 无法读取未定义的属性(读取“球形”)[关闭]
【发布时间】:2021-11-03 20:36:10
【问题描述】:

我想在搜索后滚动到壁橱标记。 Console.log 显示 pin[i] = (50.296322, 9.112082) 和事件 (50.937531, 6.9602786)。 我尝试了很多但总是得到:Cannot read properties of undefined (reading 'spherical')

function geocodeAddress(geocoder, resultsMap){
var address = document.getElementById('address').value;
geocoder.geocode({'address': address}, function(results, status){
    if (status === 'OK') { 
        function find_closest_marker(event){
            var distances = [];
            var closest = -1;
            for(i = 0; i < pins.length; i++){
                var d = google.maps.geometry.spherical.computeDistanceBetween(pins[i].position, event);
                distances[i] = d;
                if (closest == -1 || d < distances[closest]){
                    closest = i;
                }
            }
            resultsMap.setCenter(pins[closest].position);
        } 
        find_closest_marker(results[0].geometry.location);          
    } else {
        alert('Geocode was not successful for the following reason: ' + status);
    }
});

}

【问题讨论】:

  • 你没有包含几何库。
  • 谢谢您的提示

标签: javascript google-maps google-cloud-platform


【解决方案1】:

https://maps.googleapis.com/maps/api/js?key=somesecrectkey&libraries=geometry&callback=initMap

感谢您的提示,它现在正在工作。

【讨论】:

    猜你喜欢
    • 2017-04-03
    • 1970-01-01
    • 2017-11-09
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 2018-04-06
    相关资源
    最近更新 更多