gpt4 book ai didi

javascript - 如何创建缩放适合谷歌地图标记?

转载 作者:行者123 更新时间:2023-11-28 04:35:23 25 4
gpt4 key购买 nike

我在谷歌地图标记适合 Zoom 时遇到了一些麻烦。我正在以 Xml 格式动态获取经纬度数据。

这是我的代码。我需要一些帮助,请任何人帮助。

var markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function(markerElem) {
var id = markerElem.getAttribute('id');
var name = markerElem.getAttribute('name');
var address = markerElem.getAttribute('address');
var type = markerElem.getAttribute('user_type');
var point = new google.maps.LatLng(
parseFloat(markerElem.getAttribute('lat')),
parseFloat(markerElem.getAttribute('lng')));


// var res = point.split({lat: lng:});

var infowincontent = document.createElement('div');
var strong = document.createElement('strong');
strong.textContent = name
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));

var text = document.createElement('text');


text.textContent = address//address view
infowincontent.appendChild(text);
var icon = customLabel[type] || {};
var icons = ["http://maps.google.com/mapfiles/ms/icons/green.png","http://maps.google.com/mapfiles/ms/icons/blue.png",
"http://maps.google.com/mapfiles/ms/icons/yellow.png"
];



var marker = new google.maps.Marker({
map: map,
//icon:icons[i],
icon:icons[Math.floor(Math.random()*icons .length)],
position: point,
label: icon.label
});

最佳答案

您可以使用fitBounds()方法:

var bounds = new google.maps.LatLngBounds();

//extend the bounds for each marker that you add
bounds.extend(marker1.getPosition());
bounds.extend(marker2.getPosition());

//update the map to fit the bounds
map.fitBounds(bounds);

关于javascript - 如何创建缩放适合谷歌地图标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44261416/

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