How to get Google Maps API to set the correct zoom level for a country?

后端 未结 4 623
长情又很酷
长情又很酷 2020-11-28 02:30

Is there a was of automatically setting the zoom level based on the size of the country that the map has been centered on?

maps.google.com does exactly what I need,

4条回答
  •  失恋的感觉
    2020-11-28 03:16

    If you don't use google maps api and just use their geocoding you can use this formula:

    var url = 'http://maps.google.com/maps/geo?q=YOUR_QUERY&output=json&oe=utf8&sensor=false&key=YOUR_KEYback=geoCodeDone';
    jQuery.getScript(url);
    
    
    function geoCodeDone(data)
    {
        if (data.Status.code == 200)
        {
            lng = data.Placemark[0].Point.coordinates[0];
            lat = data.Placemark[0].Point.coordinates[1];
    
            var east  = data.Placemark[0].ExtendedData.LatLonBox.east;
            var west  = data.Placemark[0].ExtendedData.LatLonBox.west;
    
            var zoom = 11 - Math.round(Math.log(Math.abs(west-east))/Math.log(2));
        }
    }
    

提交回复
热议问题