简体   繁体   English

如何在Google Maps JS API中获取仍包含一组Lat / Long坐标的最小LatLngBounds?

[英]How can I get the smallest LatLngBounds that still contains a set of Lat/Long Coordinates in Google Maps JS API?

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? 如何找到包含所有坐标的最小边界框(LatLngBounds)?

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() . 除了@Crescent Fresh上面指出Stack Overflow帖子 (使用v2 API)之外,你想要使用的方法是LatLngBounds.extend()

Here's a complete example, using the v3 API : 以下是使用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: 截图:

谷歌地图LatLngBounds.extend()演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 世界的最大纬度和经度边界 - Google Maps API LatLngBounds() - Maximum Lat and Long bounds for the world - Google Maps API LatLngBounds() 如何使用谷歌地图JS api从给定的纬度和经度坐标找到半径(比如10米)中的坐标(纬度和经度) - How to find the coordinates (lat & long) in a radius (say like 10 meters) from a given lat & long coordinate using google maps JS api Google Maps API,如何从机场代码获取时间? - Google Maps API, how to get lat long from airport code? Google Maps API:分别获取经度/纬度 - Google maps API: get lat / long separately 如何从DirectionsResult对象中提取所有经纬度坐标(Google Maps Javascript路线API) - How to extract all lat-long coordinates from DirectionsResult object (Google Maps Javascript directions API) Bing地图。 如何通过lat和long获取地址? - Bing maps. How can I get address by lat and long? 将纬度/经度坐标推入Google地图数组 - Push lat/long coordinates to an array for Google maps Google Maps API - 获取lat / long之间路线的点数 - Google Maps API - Get points along route between lat/long 使用Google Maps API获取经纬度的地址 - Get address with long and lat using google maps api Google Earth JS API中的LatLngBounds? - LatLngBounds in Google Earth JS API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM