Javascript 如何让 Google Maps API 为一个国家设置正确的缩放级别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2265055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:31:23  来源:igfitidea点击:

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

javascriptgoogle-maps

提问by krishnaz

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, so, for example, if I search for Russia I get a zoom level such that Russia just fits on screen, and when I search for Cuba I get a higher zoom level so that Cuba just fits.

maps.google.com 正是我所需要的,所以,例如,如果我搜索俄罗斯,我会得到一个缩放级别,俄罗斯刚好适合屏幕,而当我搜索古巴时,我会得到更高的缩放级别,因此古巴只是适合。

Is there some way of giving the Maps Api a country location and getting an appropriate zoom level.

是否有某种方法可以为 Maps Api 提供国家/地区位置并获得适当的缩放级别。

If not, I guess that I would have to manually (ugh!) create my own table for this information. Or is this information freely available somewhere?

如果没有,我想我将不得不手动(呃!)为这些信息创建我自己的表。或者这些信息是否可以在某处免费获得?

回答by Mahesh M

For V3 this code worked for me:

对于 V3,此代码对我有用:

var geocoder = new google.maps.Geocoder();
   geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        map.fitBounds(results[0].geometry.viewport);
      }
    });

回答by Daniel Vassallo

For API v3 check this answer.

对于 API v3,请检查此答案

You can use the Google Maps Client-side Geocoderto get the bounding box of the country, as in the following example:

您可以使用 Google Maps Client-side Geocoder获取国家/地区的边界框,如下例所示:

// API version 2
var geocoder = new GClientGeocoder();

geocoder.getLocations("Russia", function (locations) { 

    var north = locations.Placemark[0].ExtendedData.LatLonBox.north;
    var south = locations.Placemark[0].ExtendedData.LatLonBox.south;
    var east  = locations.Placemark[0].ExtendedData.LatLonBox.east;
    var west  = locations.Placemark[0].ExtendedData.LatLonBox.west;

    var bounds = new GLatLngBounds(new GLatLng(south, west), 
                                   new GLatLng(north, east));

    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
});

// API version 3
// ... set north, south, east and west ...
var bounds = new google.maps.LatLngBounds(new google.maps.LatLng(south, west), 
                                          new google.maps.LatLng(north, east));
map.fitBounds(bounds);

The screenshots below show the results of the above technique when searching for Russia and Cuba:

下面的截图显示了上述技术在搜索俄罗斯和古巴时的结果:

Zoom on Russia and Cuba

放大俄罗斯和古巴

回答by Mihai Cr?i??

If you don't want to use the geocoder client from google, because of usage limitations you could use your own list. You can get one from this github repo.

如果您不想使用 google 的地理编码器客户端,由于使用限制,您可以使用自己的列表。你可以从这个github repo得到一个。

Here is a code example using jQuery's getJSON function and google maps API v3:

下面是一个使用 jQuery 的 getJSON 函数和谷歌地图 API v3 的代码示例:

    function initialize() {
      // read the list of countries
     $.getJSON('countries.json', function (countries) {

         // will use the country with index 40 (Cyprus)
         var index_country = 40;
         var myOptions = {
             center: new google.maps.LatLng(
                 countries[index_country].center_lat,
                 countries[index_country].center_lng),
         }
         var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

         // set the bounds of the map
         var bounds = new google.maps.LatLngBounds(
             new google.maps.LatLng(countries[index_country].sw_lat, countries[index_country].sw_lng),
             new google.maps.LatLng(countries[index_country].ne_lat, countries[index_country].ne_lng) );

         map.fitBounds(bounds);
     });
 }

回答by Denis Shishkov

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

如果您不使用 google maps api 而只使用他们的地理编码,则可以使用以下公式:

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));
    }
}