【问题标题】:How do I lock the country down in Google Maps如何在谷歌地图中锁定国家
【发布时间】:2009-07-08 13:19:48
【问题描述】:

我怎样才能让你不能在谷歌地图中拖到英国以外的地方? 你能把它锁定在某个区域吗,因为我希望能够在英国周围拖动,但不能在外面拖动。

克里斯

【问题讨论】:

    标签: api google-maps locking geolocation


    【解决方案1】:

    有关如何执行此操作的文章,请参阅 Google Maps API Range Limiting

    代码都在the example中。

    【讨论】:

    • 甜的;忘了检查他的网站。在谷歌中找不到答案;我今天一定是在遭受关键字迟缓。 :op 塔
    【解决方案2】:

    较早接受的答案在 v3 中已弃用,但在 2019 年 2 月,Google 提供了“限制地图边界”API。

    https://developers.google.com/maps/documentation/javascript/examples/control-bounds-restriction

    锁定波兰边界的示例代码是:

    var centerPoland = { lat: 52.407578, lng: 19.393004 };
    
    function initMap() {
      var options = {
        zoom: 7,
        center: centerPoland,
        restriction: {
          latLngBounds: {
            east:  24.111384999999927,
            north: 54.83666,
            south: 49.002914,
            west: 14.149166000000037
          },
          strictBounds: true
        },
        scaleControl: true
      };
      // Create map
      map = new google.maps.Map(document.getElementById("map"), options);
    }
    

    请注意,您无法缩小以查看整个国家(包括邻国)。那是因为strictBounds: true 设置为false 可以缩小,但平移限制仍然存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多