如何在Google Maps JS API中获取仍包含一组纬度/经度坐标的最小LatLngBounds? [英] How can I get the smallest LatLngBounds that still contains a set of Lat/Long Coordinates in Google Maps JS API?

查看:101
本文介绍了如何在Google Maps JS API中获取仍包含一组纬度/经度坐标的最小LatLngBounds?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在地图上绘制一组坐标以响应用户的选择,当它发生时,我想平移地图以关注这组点。我怎样才能找到包含所有坐标的最小边界框(LatLngBounds)?

解决方案

Stack Overflow post @Crescent Fresh (使用v2 API)时,您希望使用的方法是 LatLngBounds.extend()



下面是一个完整的示例,使用 v3 API

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

< script type =text / javascript>

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

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

var randomPoint,i;

(i = 0; i <10; i ++){
//在东北美洲生成10个随机点
randomPoint = new google.maps.LatLng(39.00 +(Math.random() - 0.5)* 20,
-77.00 +(Math.random() - 0.5)* 20);

//为每个随机点绘制一个标记
new google.maps.Marker({
position:randomPoint,
map:map
}) ;

//用每个随机点扩展markerBounds。
markerBounds.extend(randomPoint);
}

//在结尾处,markerBounds将是包含
//的最小边界框//我们的10个随机点

//最后我们可以调用Map.fitBounds()方法将地图设置为适合
//我们的markerBounds
map.fitBounds(markerBounds);

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

截图:


I need to plot a set of coordinates on the map in response to a user selection, and when it happens, I'd like to pan the map to focus on that set of points. How can I find the smallest bounding box (LatLngBounds) that contains all of the coordinates?

解决方案

In addition to the Stack Overflow post which @Crescent Fresh pointed to above (which is using the v2 API), the method you'd want to use is the LatLngBounds.extend().

Here's a complete example, using the v3 API:

<!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 America
     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>

Screenshot:

这篇关于如何在Google Maps JS API中获取仍包含一组纬度/经度坐标的最小LatLngBounds?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆