gpt4 book ai didi

javascript - 在谷歌地图上适合标记

转载 作者:行者123 更新时间:2023-12-03 11:33:44 24 4
gpt4 key购买 nike

我一直在尝试将我的标记放在谷歌地图上。我一直在尝试使用以下内容:

map.fitBounds(latlngbounds); 

我不知道如何实现这个。如何轻松添加此内容?

到目前为止,我有以下代码:

 <script type="text/javascript">

var locations = <?php echo json_encode($js_array);?>;
var lats = <?php echo json_encode($lats);?>;
var lats = <?php echo json_encode($iphoto);?>;

var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(<?php echo $latitude;?>, <?php echo $longitude;?>),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {
console.log(locations[i].lat);
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i].latitude, locations[i].longitude),
map: map,
animation: google.maps.Animation.DROP,
icon: {size: new google.maps.Size(30, 30),
scaledSize: new google.maps.Size(30, 30),
url: locations[i].iprofilephoto,}
});

google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent("<div><img style='width:40px;height:40px' src='"+locations[i].iprofilephoto+"'>"+locations[i].name+"</div><div><img style='width:100px;height:100px' src='"+locations[i].iphoto+"'></div>");
infowindow.open(map, marker);
}
})(marker, i));
}

最佳答案

您需要使用所有纬度来扩展边界,如下所示。

var latlngbound = new google.maps.LatLngBounds();
for (i = 0; i < locations.length; i++) {
// CREATE LATLNG OBJECT AND USE IT TO EXTEND THE LATLNGBOUNDS
var latlng = new google.maps.LatLng(locations[i].latitude, locations[i].longitude);
latlngbound .extend(latlong);

marker = new google.maps.Marker({
position: latlng,
map: map,
animation: google.maps.Animation.DROP,
icon: {size: new google.maps.Size(30, 30),
scaledSize: new google.maps.Size(30, 30),
url: locations[i].iprofilephoto,}
});

最后您需要使边界适合 map 。

map.fitBounds(latlngbounds); 

关于javascript - 在谷歌地图上适合标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26624515/

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