首页 > 解决方案 > 如何修复不是 LatLng 或 LatLngLiteral:不是对象问题 - Google 地图

问题描述

此功能的想法是,当用户输入一个位置时,我们在该位置添加一个标记并平移所述标记并放大该标记的位置。然而,所有这些工作都相对良好。它给了我这个错误“ Uncaught Kc {message:”not a LatLng or LatLngLiteral: not an Object”, name: “InvalidValueError”。”

if(map.getZoom() == 2) {

    map.setCenter({lat:data.address_lat2, lng:data.address_lng2});
    map.panTo(marker.position);
    smoothZoom(map, 11, map.getZoom());

    var location = new google.maps.LatLng({lat:data.address_lat2, lng:data.address_lng2});
    var bounds = new google.maps.LatLngBounds();
    bounds.extend(location.position);
    map.fitBounds(bounds);
}

这是我的 smoothZoom 功能

function smoothZoom (map, max, cnt) {
   if (cnt >= max) {
        return;
    } else {
        z = google.maps.event.addListener(map, 'zoom_changed', function(event){
        google.maps.event.removeListener(z);
        smoothZoom(map, max, cnt + 1);
    });
        setTimeout(function(){map.setZoom(cnt)}, 150);
    }
}

标签: javascriptgoogle-mapsgoogle-maps-api-3maps

解决方案


原来这行代码 bounds.extend(location.position); 应该是 bounds.extend({lat:data.address_lat2,lng:data.address_lng2});


推荐阅读