【发布时间】:2023-03-19 00:43:01
【问题描述】:
我正在使用 google maps javascript API,目前我正在尝试创建一个 LatLngBounds 对象,以便我可以根据数组中的位置将它应用到我的 google 地图。
我正在寻找西南和东北大多数点,从中创建 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 对象创建边界会引发错误似乎很奇怪。
我错过了什么导致此错误?
【问题讨论】:
标签: javascript google-maps latitude-longitude