gpt4 book ai didi

google-maps - 谷歌地图 V3 中心多个标记和自动缩放问题

转载 作者:行者123 更新时间:2023-12-01 15:30:59 26 4
gpt4 key购买 nike

我需要知道如何将所有标记居中并自动缩放 map 以适应我接下来的 map 中的所有标记。几天来我一直在努力解决这个问题,请帮助我。

我的代码

<script type="text/javascript">  
var map;
var address = new Array();
//we can also create following array with a for loop
address[0] = '798 9th Ave, New York, NY';
address[1] = '42 E 29th St, New York, NY';
address[2] = '56 W 25th St, New York, NY';
address[3] = '26 W 21th St, New York, NY';

function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {zoom: 15, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

for (var i=0; i<=address.length; i++) {
geocoder.geocode({'address': address[i]}, function(results, status) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({map: map, position: results[0].geometry.location});
});
}
}
</script>

最佳答案

<script type="text/javascript">  
var map;
var address = new Array();
//we can also create following array with a for loop
address[0] = '798 9th Ave, New York, NY';
address[1] = '42 E 29th St, New York, NY';
address[2] = '56 W 25th St, New York, NY';
address[3] = '26 W 21th St, New York, NY';

function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {zoom: 15, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
//Mod
var bounds = new google.maps.LatLngBounds();

for (var i=0; i<=address.length; i++) {
geocoder.geocode({'address': address[i]}, function(results, status) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({map: map, position: results[0].geometry.location});
//Mod
bounds.extend(marker.getPosition());
});
}
//Mod
map.fitBounds(bounds);
}
</script>

关于google-maps - 谷歌地图 V3 中心多个标记和自动缩放问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7785767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com