OSMDroid 将地图限制在 North/South
OSMDroid Limit the map in North/South
我正在使用 OSMDroid API,我想知道是否有办法限制地图的南北方向。我的意思是,这是否可以防止地图在 Y 轴上无休止地重复自己。
我在Github的"osmdroid"项目上发现了这个问题,但是补丁代码太旧,无法应用到新版本的osmdroid(4.2)
编辑
我尝试了 setScrollableAreaLimit 方法,但在左上角有一个错误(可能)。当我接近它的尽头时,地图跳到另一边。
提前致谢
我修改了classMapView
中的两行。
我将 x += worldSize;
替换为 x=0;
,将 y += worldSize;
替换为 y=0;
public void scrollTo(int x, int y) {
final int worldSize = TileSystem.MapSize(this.getZoomLevel(false));
while (x < 0) {
//x += worldSize;
x=0;
}
while (x >= worldSize) {
x -= worldSize;
}
while (y < 0) {
// y += worldSize;
y=0;
}
while (y >= worldSize) {
y -= worldSize;
}
[...]
}
我正在扩展 MapView 并覆盖到 scrollTo()
public void scrollTo(int x, int y) {
final int worldSize = TileSystem.MapSize(this.getZoomLevel(false));
if(y < 0) { // when over north pole
y = 0; // scroll to north pole
}else if(y + getHeight() >= worldSize) { // when over south pole
y = worldSize-getHeight() - 1; // scroll to south pole
}
super.scrollTo(x,y);
}
我能够通过扩展@kenji 的答案来克服这个问题。使用子类方法,我发现快速缩小有时会将地图重置为南极。为了防止这种情况,需要先缩小 y 值(就像 v5.6.5 中的 super class 实现一样)。
@Override
public void scrollTo(int x, int y) {
final int worldSize = TileSystem.MapSize(this.getZoomLevel(false));
final int mapViewHeight = getHeight();
// Downscale the y-value in case the user just zoomed out.
while (y >= worldSize) {
y -= worldSize;
}
if (y < 0) {
y = 0;
} else if ((y + mapViewHeight) > worldSize) {
y = worldSize - mapViewHeight;
}
super.scrollTo(x, y);
}
使用 setScrollableAreaLimit。
BoundingBoxE6 bbox_bariloche = new BoundingBoxE6(-41.033770, -71.591065, -41.207056,-71.111444);
map.setScrollableAreaLimit(bbox_bariloche);
我正在使用 OSMDroid API,我想知道是否有办法限制地图的南北方向。我的意思是,这是否可以防止地图在 Y 轴上无休止地重复自己。
我在Github的"osmdroid"项目上发现了这个问题,但是补丁代码太旧,无法应用到新版本的osmdroid(4.2)
编辑
我尝试了 setScrollableAreaLimit 方法,但在左上角有一个错误(可能)。当我接近它的尽头时,地图跳到另一边。
提前致谢
我修改了classMapView
中的两行。
我将 x += worldSize;
替换为 x=0;
,将 y += worldSize;
替换为 y=0;
public void scrollTo(int x, int y) {
final int worldSize = TileSystem.MapSize(this.getZoomLevel(false));
while (x < 0) {
//x += worldSize;
x=0;
}
while (x >= worldSize) {
x -= worldSize;
}
while (y < 0) {
// y += worldSize;
y=0;
}
while (y >= worldSize) {
y -= worldSize;
}
[...]
}
我正在扩展 MapView 并覆盖到 scrollTo()
public void scrollTo(int x, int y) {
final int worldSize = TileSystem.MapSize(this.getZoomLevel(false));
if(y < 0) { // when over north pole
y = 0; // scroll to north pole
}else if(y + getHeight() >= worldSize) { // when over south pole
y = worldSize-getHeight() - 1; // scroll to south pole
}
super.scrollTo(x,y);
}
我能够通过扩展@kenji 的答案来克服这个问题。使用子类方法,我发现快速缩小有时会将地图重置为南极。为了防止这种情况,需要先缩小 y 值(就像 v5.6.5 中的 super class 实现一样)。
@Override
public void scrollTo(int x, int y) {
final int worldSize = TileSystem.MapSize(this.getZoomLevel(false));
final int mapViewHeight = getHeight();
// Downscale the y-value in case the user just zoomed out.
while (y >= worldSize) {
y -= worldSize;
}
if (y < 0) {
y = 0;
} else if ((y + mapViewHeight) > worldSize) {
y = worldSize - mapViewHeight;
}
super.scrollTo(x, y);
}
使用 setScrollableAreaLimit。
BoundingBoxE6 bbox_bariloche = new BoundingBoxE6(-41.033770, -71.591065, -41.207056,-71.111444);
map.setScrollableAreaLimit(bbox_bariloche);