2010-11-04 72 views
1

我在我的网站上有Google地图(v3),我想知道当前缩放比例是多少。问题是用户可以改变它的缩放比例,因此可以改变比例。我如何知道我的Google地图当前所在的实际比例?

我需要的信息是我的地图的公里数的实际宽度。我知道我可以使用界限......但还有其他方法吗?我真的不想使用界限。

谢谢!

+0

好的。我决定使用Bounds:P。无论如何,有没有办法获得实际的规模? – 2010-11-04 01:24:55

+0

随着墨卡托投影尺度随纬度发生变化,所以这不像听起来那么简单。 – Ossama 2010-11-08 06:45:24

+0

你说得对。但我不明白为什么Google会提供一个缩放图形,您可以根据需要将其添加到地图中。所以它看起来像谷歌可以告诉你真实的地图规模。 – 2010-11-09 13:36:03

回答

3

所以你可以调用map.getBounds()。然后,您可以使用google.maps.geometry.spherical类的功能,如computeDistanceBetweencomputeLength。像这样的东西,我认为应该给你你需要的东西:

<!DOCTYPE html> 
<html> 
<head> 
<title></title> 

<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<style type="text/css"> 
html { height: 100% } 
body { height: 100%; margin: 0; padding: 0 } 
#map_canvas { height: 100% } 
</style> 

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry"></script> 

<script type="text/javascript"> 
    function initialize() { 
     var homeLatlng = new google.maps.LatLng(51.476706,0); // London 

     var myOptions = { 
      zoom: 15, 
      center: homeLatlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

     var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 


     google.maps.event.addListener(map, 'bounds_changed', function() {   
      // find out the map's bounds: 
      var bounds = map.getBounds(); 

      // from that, get the coordinates for the NE and SW corners 
      var NE = bounds.getNorthEast(); 
      var SW = bounds.getSouthWest(); 

      // from that, figure out the latitudes and the longitudes 
      var lat1 = NE.lat(); 
      var lat2 = SW.lat(); 

      var lng1 = NE.lng(); 
      var lng2 = SW.lng(); 

      // construct new LatLngs using the coordinates for the horizontal distance between lng1 and lng2 
      var horizontalLatLng1 = new google.maps.LatLng(lat1,lng1); 
      var horizontalLatLng2 = new google.maps.LatLng(lat1,lng2); 

      // construct new LatLngs using the coordinates for the vertical distance between lat1 and lat2 
      var verticalLatLng1 = new google.maps.LatLng(lat1,lng1); 
      var verticalLatLng2 = new google.maps.LatLng(lat2,lng1); 

      // work out the distance horizontally 
      var horizontal = google.maps.geometry.spherical.computeDistanceBetween(horizontalLatLng1,horizontalLatLng2); 

      // work out the distance vertically 
      var vertical = google.maps.geometry.spherical.computeDistanceBetween(verticalLatLng1,verticalLatLng2); 

      // round to kilometres to 1dp 
      var horizontalkm = convertMetresToKm(horizontal); 
      var verticalkm = convertMetresToKm(vertical); 

      alert(horizontalkm + ' km horizontal, ' + verticalkm + ' km vertical'); 
     }); 
    } 

    function convertMetresToKm(metres) { 
     return Math.round(metres/1000 *10)/10; // distance in km rounded to 1dp 
    } 


    google.maps.event.addDomListener(window, 'load', initialize); 
</script> 
</head> 
<body> 
    <div id="map_canvas"></div> 
</body> 
</html> 
+0

我很欣赏你的回复。我对谷歌地图api v3很陌生,我还不知道如何通过地图的西南角和东北角获得比例尺。 – huanhuanw 2013-10-16 08:46:34

+0

查看我的更新回答 – duncan 2013-10-16 09:43:00

+0

谢谢,但它是没有工作的。我想从scaleControl中获得比例值,例如1km/2000ft,200m/1000ft.Sorry,我没有足够的声望来发布图像。 – huanhuanw 2013-10-16 10:11:15

相关问题