javascript invalidValueError : setCenter : 不是 LatLng 或 LatLngLiteral 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23003104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
invalidValueError : setCenter : not a LatLng or LatLngLiteral Object
提问by Keluang Yang Ko Kenal
I'm new to google map api v3
. I already establish a decent google map with 2 marker but when I use google.maps.latlngBounds
, it return me an error
我是 google map 的新手api v3
。我已经用 2 个标记建立了一个不错的谷歌地图,但是当我使用时google.maps.latlngBounds
,它返回一个错误
invalidValueError : setCenter : not a LatLng or LatLngLiteral Object
.
invalidValueError : setCenter : not a LatLng or LatLngLiteral Object
.
How can I fix this? I just want to centre my map around these markers.
我怎样才能解决这个问题?我只想将我的地图以这些标记为中心。
function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var myLatlng2 = new google.maps.LatLng(-25.573688, 132.567212);
var mapOptions = {
center: myLatlng,
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
try {
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
var marker = new google.maps.Marker({
position: myLatlng
});
var marker2 = new google.maps.Marker({
position: myLatlng2
});
trackerMarkerArray.push(marker);
trackerMarkerArray.push(marker2);
var latlngbound = new google.maps.LatLngBounds ();
for (var i = 0; i<trackerMarkerArray.length; i++){
trackerMarkerArray[i].setMap(map);
latlngbound.extend(trackerMarkerArray[i].position);
}
//render new map and center around group of marker
map.setCenter(latlngbound);
map.fitBounds(latlngbound);
} catch (err){
alert(err);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
回答by geocodezip
To "fit" the map to a set of markers added to a google.maps.LatLngBoundsobject use map.fitBounds(bounds), not map.setCenter().
要将地图“适合”添加到google.maps.LatLngBounds对象的一组标记,请使用 map.fitBounds(bounds),而不是 map.setCenter()。
回答by lccarrasco
map.setCenter(latlngbound);
map.setCenter takes a LatLng as a parameter, you're passing a LatLngBounds, I think you want to do it like this:
map.setCenter 将 LatLng 作为参数,您传递的是 LatLngBounds,我认为您想这样做:
map.setCenter(latlngbound.getCenter());