Javascript 尝试从有效的LatLngBounds对象创建LatLngBounds对象时出错
我正在使用GoogleMapsJavaScriptAPI,目前我正在尝试创建一个LatLngBounds对象,以便根据数组中的位置将其应用于我的GoogleMap 我正在寻找西南部和东北部的大多数点,从这些点创建LatLng对象,然后尝试使用这些点创建LatLangBounds对象:Javascript 尝试从有效的LatLngBounds对象创建LatLngBounds对象时出错,javascript,google-maps,latitude-longitude,Javascript,Google Maps,Latitude Longitude,我正在使用GoogleMapsJavaScriptAPI,目前我正在尝试创建一个LatLngBounds对象,以便根据数组中的位置将其应用于我的GoogleMap 我正在寻找西南部和东北部的大多数点,从这些点创建LatLng对象,然后尝试使用这些点创建LatLangBounds对象: centerMap: function (){ var lowestLatitude = this.locations.reduce(function (carry, nextLocation, index
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);
}
centerMap:函数(){
var lowestlatude=this.locations.reduce(函数(进位、下一个位置、索引){
返回进位nextLocation.position.lng&&carry!==null?进位:nextLocation.position.lat;
},空);
var highestLongitude=this.locations.reduce(函数(进位、下一个位置、索引){
返回进位>nextLocation.position.lng&&carry!==null?进位:nextLocation.position.lng;
},空);
调试器;
var soutwest=new google.maps.LatLng({lat:lowestlatude,lng:lowestLongitude});
var northEast=new google.maps.LatLng({lat:highestLatitude,lng:highestLongitude});
var-bounds=新的google.maps.LatLngBounds({sw:southern,ne:neuthern});
this.map.fitbunds(bounds);
}
我遇到的错误是在创建LatLngBounds实例时。我得到一个错误:
未捕获的InvalidValueError:不是LatLng或LatLngLiteral:在属性lat:不是数字中
问题是,西南和东北的LatLng实例是在没有错误的情况下创建的,因此尝试从两个有效的LatLng对象创建边界会抛出错误,这似乎很奇怪
导致此错误的原因是我缺少了什么?看起来您正在向构造函数传递一个对象,而不是两个参数-您是否尝试过以下方法:
var bounds = new google.maps.LatLngBounds(southWest, northEast);
看起来您正在将一个对象而不是两个参数传递给构造函数-您是否尝试过以下方法:
var bounds = new google.maps.LatLngBounds(southWest, northEast);
查看构造函数的属性,构造函数采用两个参数,而不是具有两个属性的对象。因此,您将需要以下内容:
var bounds = new google.maps.LatLngBounds(southWest, northEast);
查看构造函数的属性,构造函数采用两个参数,而不是具有两个属性的对象。因此,您将需要以下内容:
var bounds = new google.maps.LatLngBounds(southWest, northEast);
构造函数(当前)不接受a作为参数。它需要两个数字,表示地理坐标的纬度和经度(按该顺序)。这:
应该是:
var southWest = new google.maps.LatLng(lowestLatitude, lowestLongitude);
var northEast = new google.maps.LatLng(highestLatitude, highestLongitude);
var bounds = new google.maps.LatLngBounds(southWest, northEast);
代码片段:
var地理编码器;
var映射;
函数初始化(){
var map=new google.maps.map(
document.getElementById(“地图画布”){
中心:新google.maps.LatLng(37.4419,-122.1419),
缩放:13,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var lowestlatude=bndsObj.bounds.south;
var Lowestlongite=bndsObj.bounds.west;
var highestLatitude=bndsObj.bounds.north;
var highestLongitude=bndsObj.bounds.east;
var soutwest=新的google.maps.LatLng(最低纬度,最低纬度);
var northEast=新的google.maps.LatLng(最高纬度、最高经度);
var bounds=new google.maps.LatLngBounds(西南、东北);
映射边界(bounds);
}
google.maps.event.addDomListener(窗口“加载”,初始化);
//纽约市边界
var bndsObj={
“界限”:{
“南”:40.4960439,
“西部”:-74.2557349,
“北”:40.9152555999999,
“东”:-73.7002721
}
};代码>
html,
身体,
#地图画布{
身高:100%;
宽度:100%;
边际:0px;
填充:0px
}
构造函数(当前)不将a作为参数。它需要两个数字,表示地理坐标的纬度和经度(按该顺序)。这:
应该是:
var southWest = new google.maps.LatLng(lowestLatitude, lowestLongitude);
var northEast = new google.maps.LatLng(highestLatitude, highestLongitude);
var bounds = new google.maps.LatLngBounds(southWest, northEast);
代码片段:
var地理编码器;
var映射;
函数初始化(){
var map=new google.maps.map(
document.getElementById(“地图画布”){
中心:新google.maps.LatLng(37.4419,-122.1419),
缩放:13,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var lowestlatude=bndsObj.bounds.south;
var Lowestlongite=bndsObj.bounds.west;
var highestLatitude=bndsObj.bounds.north;
var highestLongitude=bndsObj.bounds.east;
var soutwest=新的google.maps.LatLng(最低纬度,最低纬度);
var northEast=新的google.maps.LatLng(最高纬度、最高经度);
var bounds=new google.maps.LatLngBounds(西南、东北);
映射边界(bounds);
}
google.maps.event.addDomListener(窗口“加载”,初始化);
//纽约市边界
var bndsObj={
“界限”:{
“南”:40.4960439,
“西部”:-74.2557349,
“北”:40.9152555999999,
“东”:-73.7002721
}
};代码>
html,
身体,
#地图画布{
身高:100%;
宽度:100%;
边际:0px;
填充:0px
}
Aww你对我来说太快了+1.好吧,那是一个可怕的瞬间!谢谢你指出这一点。啊,你对我来说太快了+1.好吧,那是一个可怕的瞬间!谢谢你指出这一点。