我如何知道我的Google地图当前所在的实际比例? [英] How can I know the actual scale that my Google maps is currently in?

查看:181
本文介绍了我如何知道我的Google地图当前所在的实际比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

谢谢!

解决方案

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

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

< meta name =viewportcontent =initial-scale = 1.0,user-scalable = no/>
< style type =text / css>
html {height:100%}
body {height:100%;保证金:0; padding:0}
#map_canvas {height:100%}
< / style>

< script type =text / javascriptsrc =http://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry>< /脚本>

< script type =text / javascript>
函数initialize(){
var homeLatlng = new google.maps.LatLng(51.476706,0); //伦敦

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

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

$ b google.maps.event.addListener(map,'bounds_changed',function(){
//找出地图边界:
var bounds = map .getBounds();

//从中得到NE和SW角的坐标
var NE = bounds.getNorthEast();
var SW = bounds.getSouthWest ();

//从中找出纬度和经度
var lat1 = NE.lat();
var lat2 = SW.lat();

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

//使用水平距离的坐标构造新的LatLngs lng1和lng2
var horizo​​ntalLatLng1 = new google.maps.LatLng(lat1,lng1);
var horizo​​ntalLatLng2 = new google.maps.LatLng(lat1,lng2);

/ /使用垂直距离的坐标构造新的LatLng n lat1和lat2
var verticalLatLng1 = new google.maps.LatLng(lat1,lng1);
var verticalLatLng2 = new google.maps.LatLng(lat2,lng1);

//计算水平距离
var horizo​​ntal = google.maps.geometry.spherical.computeDistanceBetween(horizo​​ntalLatLng1,horizo​​ntalLatLng2);

//垂直计算距离
var vertical = google.maps.geometry.spherical.computeDistanceBetween(verticalLatLng1,verticalLatLng2);

//圆到公里到1dp
var horizo​​ntalkm = convertMetresToKm(horizo​​ntal);
var verticalkm = convertMetresToKm(vertical);

alert(horizo​​ntalkm +'km horizo​​ntal,'+ verticalkm +'km vertical');
});


function convertMetresToKm(meters){
return Math.round(meters / 1000 * 10)/ 10; //以km为单位的距离舍入为1dp
}


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


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!

解决方案

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>

这篇关于我如何知道我的Google地图当前所在的实际比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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