【问题标题】:How to fix a not a LatLng or LatLngLiteral: not an Object issue - Google Maps如何修复不是 LatLng 或 LatLngLiteral:不是对象问题 - Google 地图
【发布时间】:2019-06-18 11:21:04
【问题描述】:

此功能的想法是,当用户输入一个位置时,我们会在该位置添加一个标记,然后平移所述标记并放大该标记的位置。然而,所有这些都工作得很好。它给了我这个错误“ 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);
}

这是我的平滑缩放功能

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);
    }
}

【问题讨论】:

  • 请提供minimal reproducible example来证明您的问题。
  • 这是一个相当大的代码库,有些方面依赖于其他方面。尤其是输入邮政编码或位置,因此提供一个最小的示例将需要相当长的时间。当我继续我一天的工作时间时,我会继续这样做。

标签: javascript google-maps google-maps-api-3 maps


【解决方案1】:

google.maps.LatLng 没有将google.maps.LatLngLiteral 作为参数的构造函数。这是无效的语法:

var location = new google.maps.LatLng({lat:data.address_lat2, lng:data.address_lng2});

应该是:

var location_place = new google.maps.LatLng(data.address_lat2, data.address_lng2);

或:

var location_place = {lat:parseFloat(data.address_lat2), lng:parseFloat(data.address_lng2)};

(如果是字符串,你只需要parseFloat

另外: location 不是一个好的变量名。 (见ref1ref2

【讨论】:

  • 感谢您的评论,我已经尝试了您的建议,但错误仍然返回。普遍感到困惑。
  • 这不可能。错误是否来自代码中的另一行?确保将上述内容应用于所有代码。如果您仍有问题,请使用Minimal, Complete, and Verifiable example 更新您的问题,以便重现问题。
  • 我亲自对代码进行了一点点注释,并将其缩小到几行代码,这就是 var location = {lat:parseFloat(data.address_lat2), lng:parseFloat(data.address_lng2) }; bounds2.extend(location.position);
  • location 不是一个好的变量名。 (见ref1ref2
  • 幸运的是这不是问题,但我明白为什么它不是一个好的变量名。不过感谢您的帮助。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 2016-01-03
    • 1970-01-01
    相关资源
    最近更新 更多