Dynamic google map with custom tiles prevent repeating pan

后端 未结 3 972
梦如初夏
梦如初夏 2021-02-09 19:47

I have a dynamic tile set where I do NOT want to allow panning outside of its bounds.

The below code gets me close, but the user can still scroll horizontally outside of

3条回答
  •  执念已碎
    2021-02-09 20:26

    Tiborg version above is the best version so far in keeping the bounds... I noticed a bug that occurs when zoom level and width/height are larger than original bounds

    I have modified his code and added a rectangle to see it in action.

    var buildBounds = function(sw, ne) {
        var swY = sw.lat();
        var swX = sw.lng();
        var neY = ne.lat();
        var neX = ne.lng();
        if (swY > neY) {
          var cY = (swY + neY) / 2;
          swY = cY;
          neY = cY;
        }
        if (swX > neX) {
          var cX = (swX + neX) / 2;
          swX = cX;
          neX = cX;
        }
        return new google.maps.LatLngBounds(
          new google.maps.LatLng(swY, swX),
          new google.maps.LatLng(neY, neX));
      }
    

    http://jsfiddle.net/p4wgjs6s/

提交回复
热议问题