How can I know the actual scale that my Google maps is currently in?

后端 未结 1 2063
旧时难觅i
旧时难觅i 2021-01-16 04:20

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 i

相关标签:
1条回答
  • 2021-01-16 04:55

    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>
    
    0 讨论(0)
提交回复
热议问题