gpt4 book ai didi

javascript - 根据其标记缩放和居中 Google map (JavaScript API V3)

转载 作者:IT王子 更新时间:2023-10-29 03:18:02 25 4
gpt4 key购买 nike

我认为标题已经足够了,我什至不知道如何为 V2 和 V1 API 执行此操作:/

谢谢:)

最佳答案

正如其他答案所建议的那样,fitBounds()方法应该可以解决问题。

考虑以下示例,它将在美国东北部生成 10 个随机点,并应用 fitBounds() 方法:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps LatLngBounds.extend() Demo</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 400px; height: 300px;"></div>

<script type="text/javascript">

var map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.TERRAIN
});

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

var randomPoint, i;

for (i = 0; i < 10; i++) {
// Generate 10 random points within North East USA
randomPoint = new google.maps.LatLng( 39.00 + (Math.random() - 0.5) * 20,
-77.00 + (Math.random() - 0.5) * 20);

// Draw a marker for each random point
new google.maps.Marker({
position: randomPoint,
map: map
});

// Extend markerBounds with each random point.
markerBounds.extend(randomPoint);
}

// At the end markerBounds will be the smallest bounding box to contain
// our 10 random points

// Finally we can call the Map.fitBounds() method to set the map to fit
// our markerBounds
map.fitBounds(markerBounds);

</script>
</body>
</html>

多次刷新此示例,没有任何标记超出视口(viewport):

fitBounds demo

关于javascript - 根据其标记缩放和居中 Google map (JavaScript API V3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3520810/

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