【问题标题】:How to get Google Maps API to set the correct zoom level for a country?如何让 Google Maps API 为一个国家/地区设置正确的缩放级别?
【发布时间】:2011-01-16 22:16:28
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: javascript google-maps


    【解决方案1】:

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

    【讨论】:

    • 完美运行。感谢您添加 V3 的更新版本。非常感谢。
    • 太棒了。适用于任何地方。城市、省、国家等
    • setCenterfitBounds 的参数对吗?我似乎无法让这个工作,得到一个 JS 错误说“递归太多” - map.setCenter(lat, lon); 工作。
    • address 可以只是一个国家的名字吗?
    • 人们真的认为这很好吗? (不是答案)但我的意思是谷歌的实际实施?一些国家几乎不占用任何屏幕。有没有办法设置它并说“占用我容器的 80%”肯定是以某种方式实现的?
    【解决方案2】:

    对于 API v3,请检查 this answer

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

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

    【讨论】:

    • 非常好,非常感谢。我在 GWT 中工作,它还不能访问 ExtendedData,所以我必须创建一些 JSNI 方法来做到这一点。一件事让我有一段时间:确保在调用 getBoundsZoomLevel 时地图是可见的,否则它返回的缩放级别为零。
    • @krishnaz:干得好。感谢分享getBoundsZoomLevel() 提示。
    • 我正在查看 api,但我没有看到(目前)使用 V3 执行此操作的方法。我也想为一个城市做这件事。
    • 我想这就是我要找的stackoverflow.com/questions/3245564/…
    • 我的 VIEWPORT 运气更好
    【解决方案3】:

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

    这是一个使用 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);
         });
     }
    

    【讨论】:

      【解决方案4】:

      如果您不使用 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));
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-04
        • 2013-03-28
        • 2012-12-05
        • 2011-06-20
        相关资源
        最近更新 更多