【问题标题】:Receiving "InvalidValueError: setCenter: not a LatLng or LatLngLiteral: in property lng: not a number" error even though the values are numbers收到“InvalidValueError: setCenter: not a LatLng or LatLngLiteral: in property lng: not a number”错误,即使值是数字
【发布时间】:2019-08-29 00:15:17
【问题描述】:

尝试使用 Google Maps API 初始化地图。以数字形式输入中心的纬度和经度。收到一个错误,指出它们不是数字。

var map; // Variable used to create new map
var mapDiv = document.getElementById('ippudo-map'); // Location of the map in the html

// Create a function to initialize the map

function initMap() {
  map = new google.maps.Map(mapDiv, {
    center: {lat: 42.3601, long: 71.0589},
    zoom: 8
  });
}

【问题讨论】:

  • 你解决了吗?即使我 parseFloat() 值,我也会收到此错误。不知道为什么....

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


【解决方案1】:

根据 Google Maps JavaScript API 文档,LatLngLiteral 接口有两个字段:lat 和 lng。

https://developers.google.com/maps/documentation/javascript/reference/coordinates#LatLngLiteral

在您的示例中,您使用的是long 而不是lng。您应该修复错误的字段名称

function initMap() {
    map = new google.maps.Map(mapDiv, {
        center: {lat: 42.3601, lng: 71.0589},
        zoom: 8
    });
}

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多