javascript - 尝试从有效的 LatLng 对象创建 LatLngBounds 对象时出错

标签 javascript google-maps latitude-longitude

我正在使用 google map javascript API,目前我正在尝试创建一个 LatLngBounds 对象,以便我可以根据数组中的位置将其应用到我的 google map 。

我正在找到西南和东北的最点,从中创建 LatLng 对象,然后尝试使用它们来创建 LatLangBounds 对象:

centerMap: function (){
    var lowestLatitude = this.locations.reduce(function (carry, nextLocation, index){
            return carry < nextLocation.position.lat && carry !== null ? carry : nextLocation.position.lat;
    }, null);

    var lowestLongitude = this.locations.reduce(function (carry, nextLocation, index){
            return carry < nextLocation.position.lng && carry !== null ? carry : nextLocation.position.lng;
    }, null);

    var highestLatitude = this.locations.reduce(function (carry, nextLocation, index){
            return carry > nextLocation.position.lng && carry !== null ? carry : nextLocation.position.lat;
    }, null);

    var highestLongitude = this.locations.reduce(function (carry, nextLocation, index){
            return carry > nextLocation.position.lng && carry !== null ? carry : nextLocation.position.lng;
    }, null);

    debugger;
    var southWest = new google.maps.LatLng({ lat: lowestLatitude, lng: lowestLongitude});
    var northEast = new google.maps.LatLng({ lat: highestLatitude, lng: highestLongitude});
    var bounds = new google.maps.LatLngBounds({sw: southWest, ne: northEast});
    this.map.fitBounds(bounds);
}

我遇到的错误是在创建 LatLngBounds 实例时发生的。我收到错误:

Uncaught InvalidValueError: not a LatLng or LatLngLiteral: in property lat: not a number

问题是,创建 SouthWest 和 NorthEast 的 LatLng 实例时没有出现错误,因此尝试从两个有效的 LatLng 对象创建边界会引发错误似乎很奇怪。

gif of the error happening

我错过了什么导致这个错误?

最佳答案

看起来您正在向构造函数传递一个对象而不是 2 个参数 - 您尝试过以下操作吗:

var bounds = new google.maps.LatLngBounds(southWest, northEast);

关于javascript - 尝试从有效的 LatLng 对象创建 LatLngBounds 对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35507910/

相关文章:

javascript - Webstorm - 如何为 javascript 调试配置添加浏览器参数

javascript - AJAX 重定向

javascript - 使用 Serialize() 抛出空值传递表单数据

java - 无法解析方法ComputeDistanceBetween

java - 从网络读取经纬度

javascript - 循环 JSON 对象 JavaScript

javascript - JQuery 和 Google map API 问题

android - 在 Android 中单击后使标记可拖动

mysql - 将纬度、经度的 MYSQL 5.7 点表示形式插入表中

gps - 纬度/经度元数据。 GPS 坐标 - 度、分、秒