【问题标题】:can I set automatic zoom level in google map?我可以在谷歌地图中设置自动缩放级别吗?
【发布时间】:2010-02-08 06:43:51
【问题描述】:

这可能吗,假设我在谷歌地图中搜索任何随机城市,谷歌地图自动设置缩放级别?我的意思是如何自动设置谷歌地图的缩放级别?

我正在使用谷歌地图 API。

请帮帮我。

【问题讨论】:

    标签: google-maps


    【解决方案1】:

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

    总之,您使用 Geocoder 对象通过查询字符串 (", ") 获取位置。然后使用返回的 PlaceMarker 对象来定义 LatLngBounds 对象。使用 LatLngBounds 对象作为 getBoundsZoomLevel 的参数来设置缩放级别。

    【讨论】:

      【解决方案2】:

      您可以针对某个地理位置要求尽可能高的缩放: https://developers.google.com/maps/documentation/javascript/maxzoom

      我在创建地图后集成了调用。因此,在加载地图时,它也会请求最大缩放级别,并且仅当地图的缩放级别低于地图的缩放级别时才会重置地图上的缩放级别:

      this.map = this.createMap(this.el, this.initPosition);
      var maxZoomService = new google.maps.MaxZoomService();
      maxZoomService.getMaxZoomAtLatLng(this.initPosition, _.bind(function(response) {
          if (response.status != google.maps.MaxZoomStatus.OK) {
              return;
          } else {
              console.log("GoogleMapView maxzoomlevel is " + response.zoom);
              if (response.zoom < this.map.getZoom()) {
                  this.map.setZoom(response.zoom);
              }
          }
      }, this));
      

      【讨论】:

        【解决方案3】:
        <!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" 
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
            <title>Google Maps JavaScript API Example</title>
            <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=abcdefg&sensor=true_or_false"
                    type="text/javascript"></script>
            <script type="text/javascript">
        
            function initialize() {
              if (GBrowserIsCompatible()) {
                var map = new GMap2(document.getElementById("map_canvas"));
                map.setCenter(new GLatLng(37.4419, -122.1419), 13);
                map.setUIToDefault();
              }
            }
        
            </script>
          </head>
          <body onload="initialize()" onunload="GUnload()">
            <div id="map_canvas" style="width: 500px; height: 300px"></div>
          </body>
        </html>
        

        map.setCenter() 函数可用于设置缩放级别,即 map.setCenter(GLatLng 坐标,缩放级别)

        【讨论】:

        • 这不是用户要求的。他要求一种自动的方式来做到这一点。
        猜你喜欢
        • 1970-01-01
        • 2013-02-27
        • 2012-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-08
        相关资源
        最近更新 更多