javascript - 使用 Google Maps API v3 裁剪部分 map

标签 javascript google-maps google-maps-api-3

如何设置 Google map 小部件的范围?例如,我只需要 [SW Angular : 40.5, -25.5] - [NE Angular 79.5, 178.5] 矩形。现在我正在使用另一个问题中的代码:

var strictBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(40.5, -25.5), 
    new google.maps.LatLng(79.5, 178.5)
);

// Listen for the dragend event
google.maps.event.addListener(map, 'dragend', function() {
    if (strictBounds.contains(map.getCenter())) return;

    // We're out of bounds - Move the map back within the bounds

    var c = map.getCenter(),
        x = c.lng(),
        y = c.lat(),
        maxX = strictBounds.getNorthEast().lng(),
        maxY = strictBounds.getNorthEast().lat(),
        minX = strictBounds.getSouthWest().lng(),
        minY = strictBounds.getSouthWest().lat();

        if (x < minX) x = minX;
        if (x > maxX) x = maxX;
        if (y < minY) y = minY;
        if (y > maxY) y = maxY;

        map.setCenter(new google.maps.LatLng(y, x));
  });

但它没有给出所需的效果 - 无论如何,此代码允许缩放 map 并且用户可以看到跨越严格边界的片段。

所以,我想知道,API v3中有没有任何方法可以从完整的世界地图中裁剪必要的部分?

最佳答案

我认为在您的情况下,您可以通过设置相应的 minZoom 属性来另外禁止用户缩小范围

您可以在初始化 map 时执行此操作,也可以在 map 初始化后执行此操作,如下所示:

 map.setOptions({minZoom:5});

关于javascript - 使用 Google Maps API v3 裁剪部分 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16885845/

相关文章:

google-maps - 地理位置在 Chrome 中不起作用

javascript - 如何防止自定义光标移动字母?

javascript - 使用 Javascript 将 img 元素拆分为四个大小相等的较小 img 元素

javascript - 与 Firebase 的协作实时 map 无法正常工作

javascript - 如何向 Google map 添加用户位置标记?

javascript - Google map API 门牌号未显示

javascript - TypeError : weatherItem. 坐标未定义 : coordinates: [weatherItem. coord.lon, weatherItem.coord.lat]

javascript - 修改函数以仅反转第三个字

javascript - 浏览器缓存 : same remote filename in different sites

android - Google map v2 上的动画标记