如何让Google Maps API为某个国家/地区设置正确的缩放级别?

时间:2010-02-15 09:51:45

标签: javascript google-maps

是否有根据地图所在国家/地区的大小自动设置缩放级别?

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

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

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

4 个答案:

答案 0 :(得分:57)

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

答案 1 :(得分:46)

  

对于API v3,请检查this answer

您可以使用Google地图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);

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

Zoom on Russia and Cuba

答案 2 :(得分:4)

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

以下是使用jQuery的getJSON函数和google maps 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);
     });
 }

答案 3 :(得分:2)

如果您不使用谷歌地图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));
    }
}
相关问题