我正在使用 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 对象创建边界会引发错误似乎很奇怪。
我错过了什么导致这个错误?
最佳答案
看起来您正在向构造函数传递一个对象而不是 2 个参数 - 您尝试过以下操作吗:
var bounds = new google.maps.LatLngBounds(southWest, northEast);
关于javascript - 尝试从有效的 LatLng 对象创建 LatLngBounds 对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35507910/