Javascript GoogleMapsAPIv3-fitBounds仅以一个标记为中心
我有几个标记,我检索,并希望中心地图。 是的,我已经阅读了所有其他答案,但它似乎不适合我: JS:Javascript GoogleMapsAPIv3-fitBounds仅以一个标记为中心,javascript,google-maps,google-maps-api-3,bounds,Javascript,Google Maps,Google Maps Api 3,Bounds,我有几个标记,我检索,并希望中心地图。 是的,我已经阅读了所有其他答案,但它似乎不适合我: JS: 您的代码总是使用LatLngBounds仅为一个点(最后一个标记)调用fitBounds。。。 如果您多次调用该函数,则每次调用该函数时,都会显示最后一个标记的边界。 您可以在geocoder.geocode函数外部定义markerBounds变量,以便它保留其值 var markerBounds = new google.maps.LatLngBounds(); geocoder.geocod
您的代码总是使用LatLngBounds仅为一个点(最后一个标记)调用fitBounds。。。 如果您多次调用该函数,则每次调用该函数时,都会显示最后一个标记的边界。 您可以在geocoder.geocode函数外部定义markerBounds变量,以便它保留其值
var markerBounds = new google.maps.LatLngBounds();
geocoder.geocode( {'address': loc}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var coordinate = results[0].geometry.location;
var icon = new google.maps.MarkerImage("images/icon-"+type+".png", new google.maps.Size(37, 44));
//create the marker
var marker = new google.maps.Marker({
map: map,
position: coordinate,
visible: true,
id: type,
shadow: shadow,
icon: icon
});
markerBounds.extend(coordinate);
map.fitBounds(markerBounds);
}
}
现在,markerBounds被初始化一次,并使用每个新标记进行扩展。您的代码总是使用只有一个点的LatLngBounds调用fitBounds,最后一个标记。。。 如果您多次调用该函数,则每次调用该函数时,都会显示最后一个标记的边界。 您可以在geocoder.geocode函数外部定义markerBounds变量,以便它保留其值
var markerBounds = new google.maps.LatLngBounds();
geocoder.geocode( {'address': loc}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var coordinate = results[0].geometry.location;
var icon = new google.maps.MarkerImage("images/icon-"+type+".png", new google.maps.Size(37, 44));
//create the marker
var marker = new google.maps.Marker({
map: map,
position: coordinate,
visible: true,
id: type,
shadow: shadow,
icon: icon
});
markerBounds.extend(coordinate);
map.fitBounds(markerBounds);
}
}
现在,markerBounds被初始化一次,并使用每个新标记进行扩展。实际上,您可以输出存储在边界中的坐标,以便通过以下方式进行记录:
console.log(“mapFitBounds:+bounds”)代码>
实际上,您可以输出在您的边界中存储的坐标,以便登录:
console.log(“mapFitBounds:+bounds”)代码>
是的,我也这么认为,并且我已经尝试在那里定义markerBounds(以及所有其他你能想到的地方)。但我又试了一次,但还是像你说的最后一次..嗯,真奇怪。。如果它在一个地方,它只执行一次,它应该工作。你发布的代码上面是什么?啊,我找到了,我确实在上面有一个“函数codeAddress(row){”函数。现在我在该函数之外设置了markerBounds,它可以工作了!:)是的,我也这么认为,我试着在那里定义markerBounds(以及其他任何可以放置它的地方).但我再次尝试了你的方法,但仍然像你说的那样只以最后一个为中心..嗯,这很奇怪..如果它在一个地方,它只执行一次,它应该工作。你发布的代码上面是什么?啊,我找到了它,我确实有一个“函数代码地址(行){”在它上面..现在我在该函数之外设置了markerBounds,它工作了!:)