Google 地图 - 无法读取未定义的属性(读取“球形”)

use*_*449 0 javascript google-maps google-cloud-platform

我想在搜索后滚动到壁橱标记。Console.log 显示 pin[i] = (50.296322, 9.112082) 和事件 (50.937531, 6.9602786)。我尝试了很多,但总是得到:无法读取未定义的属性(读取“球形”)

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);
    }
});
Run Code Online (Sandbox Code Playgroud)

}

use*_*449 11

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

感谢您的提示,现在可以使用了。

  • 对于那些使用 @react-google-maps/api 的人,您可以这样做: ```js const { isLoaded } = useJsApiLoader({ id: 'google-map-script', googleMapsApiKey: `${process.env. REACT_APP_GOOGLE_MAPS_API_KEY}`,库:['几何'],}) ``` (4认同)