我如何知道我的 Google 地图当前的实际比例?

发布于 2024-09-30 16:29:11 字数 158 浏览 5 评论 0原文

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

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

谢谢你!

I have a Google map (v3) on my site and I want to know what scale is my current zoom. The thing is that the user can change it's zoom so the scale can change.

The information I need is the actual width in kilometers of my map. I know I can use Bounds... but is there any other way? I really don't want to use Bounds.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难遇 2024-10-07 16:29:11

所以你可以调用map.getBounds()。然后,您可以使用 google.maps.geometry.spherical< 上的函数/a> 类,例如 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>

So you can call map.getBounds(). Then you can use functions on the google.maps.geometry.spherical class like computeDistanceBetween or computeLength. Something like this I think should give you what you need:

<!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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文