Google Maps API v2:防止滚动到地图边框上

问题描述 投票:0回答:2

使用我的 Google Map API v2 标准设置,似乎可以在现有地图数据之外滚动。滚动实际上是有限制的,但似乎超出了实际地图范围。

有没有简单的方法可以解决这个问题?

enter image description here

android google-maps
2个回答
0
投票

在我的项目中,我使用从 onCameraChange 调用的函数。它将相机移回到地图允许的区域内。

// First set the bounds
private static final LatLng NORTHEAST = new LatLng("LAT HERE", "LNG HERE");
private static final LatLng SOUTHWEST = new LatLng("LAT HERE", "LNG HERE");
private static final LatLngBounds BOUNDS = new LatLngBounds(SOUTHWEST, NORTHEAST);

方法如下:

 if (BOUNDS.contains(googleMap.getCameraPosition().target)) {
  return;
}

double x = googleMap.getCameraPosition().target.longitude;
double y = googleMap.getCameraPosition().target.latitude;

double maxX = BOUNDS.northeast.longitude;
double maxY = BOUNDS.northeast.latitude;
double minX = BOUNDS.southwest.longitude;
double minY = BOUNDS.southwest.latitude;

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


googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(y, x))); 

我希望这就是您正在寻找的。


0
投票

使用最新的地图库(或反应库,例如

@googlemaps/google-maps-services-js
),您现在可以从这里使用“限制”解决方案:

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

作为

@googlemaps/google-maps-services-js
上的反应道具:

restriction={{
  latLngBounds: {
    east: 180,
    north: 85,
    south: -85,
    west: -180,
  },
  strictBounds: false,
}}
© www.soinside.com 2019 - 2024. All rights reserved.