【问题标题】:How can I know the actual scale that my Google maps is currently in?我如何知道我的 Google 地图当前的实际比例?
【发布时间】:2011-05-04 19:33:47
【问题描述】:

我的网站上有一张 Google 地图 (v3),我想知道我当前的缩放比例是多少。问题是用户可以改变它的缩放比例,这样比例就可以改变。

我需要的信息是地图的实际宽度(以公里为单位)。我知道我可以使用 Bounds ......但是还有其他方法吗?我真的不想使用 Bounds。

谢谢!

【问题讨论】:

  • 好的。我决定使用边界:P。无论如何,有没有办法得到实际的规模?
  • 墨卡托投影比例随着纬度的变化而变化,所以这并不像听起来那么简单。
  • 你是对的。但我不明白为什么 Google 会提供一个比例图形,您可以根据需要将其添加到地图中。所以看起来谷歌可以告诉你地图的真实比例。

标签: google-maps-api-3 zooming scale


【解决方案1】:

所以你可以调用 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>

【讨论】:

  • 感谢您的回复。我是 google maps api v3 的新手,但我仍然不知道如何按地图的西南角和东北角获取比例。
  • 谢谢,但是好像不行。我想从scaleControl获取比例值,比如1km/2000ft,200m/1000ft。抱歉,我没有足够的声望来发帖图片。
  • 我不确定你的意思,也不知道如何理解。我刚刚看到“我需要的信息是我的地图的实际宽度,以公里为单位,比如50公里。”这就是上面代码的作用。
  • 另请参阅我的文章,我在这里扩展了显示地图大小的想法:duncan99.wordpress.com/2013/10/19/google-maps-size
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多