Android 将Google地图应用程序移植到Osmdroid-覆盖问题
为了使用OpenStreetMaps,我正在将一个基于谷歌地图的项目移植到Osmdroid。除了添加由许多直线和一些文本组成的覆盖外,端口工作正常。在这两个项目中,我通过计时器线程和调用redrawOverlay的处理程序添加覆盖 在OSM项目中,我的叠加只是一个完全隐藏地图的灰色正方形。如果我删除了对RedrawOverly的调用,OSM磁贴显示为OK。在下面的代码示例中,我已将覆盖代码减少到一条对角线的最小值。它在谷歌应用程序中运行良好,覆盖了地图块。com.google.android.maps.Overlay有一个draw方法,OSM有一个onDraw,所以我在OSM版本中有:Android 将Google地图应用程序移植到Osmdroid-覆盖问题,android,Android,为了使用OpenStreetMaps,我正在将一个基于谷歌地图的项目移植到Osmdroid。除了添加由许多直线和一些文本组成的覆盖外,端口工作正常。在这两个项目中,我通过计时器线程和调用redrawOverlay的处理程序添加覆盖 在OSM项目中,我的叠加只是一个完全隐藏地图的灰色正方形。如果我删除了对RedrawOverly的调用,OSM磁贴显示为OK。在下面的代码示例中,我已将覆盖代码减少到一条对角线的最小值。它在谷歌应用程序中运行良好,覆盖了地图块。com.google.android.m
private MapView mv;
private MapOverlay mmapOverlay = null;
private void redrawOverlay() {
gPt = mv.getMapCenter();
if (mmapOverlay == null)
mmapOverlay = new MapOverlay(getApplicationContext());
List<Overlay> listOfOverlays = mv.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mmapOverlay);
mv.invalidate();
}
public class MapOverlay extends org.osmdroid.views.overlay.Overlay {
public MapOverlay(Context ctx) {
super(ctx);
// TODO Auto-generated constructor stub
}
@Override
public void onDraw(Canvas canvas, MapView mapView) {
Paint lp3;
lp3 = new Paint();
lp3.setColor(Color.RED);
lp3.setAntiAlias(true);
lp3.setStyle(Style.STROKE);
lp3.setStrokeWidth(1);
lp3.setTextAlign(Paint.Align.LEFT);
lp3.setTextSize(12);
canvas.drawLine(10, 10, 150, 150, lp3);
}
“重绘覆盖”是相同的,只是覆盖的实例化是:
mmapOverlay = new MapOverlay();
我们将非常感谢您的所有建议
库尔茨马克的更新问题:
感谢您迄今为止的帮助,我知道您是Osmdroid的作者之一。我喜欢到目前为止它所做的一切。我想抑制双击时出现的“跳转并放大”。我希望它什么也不做。我认为它可能在你的源代码中点击了这一点,并进行了缩放修正:
private class MapViewDoubleClickListener implements GestureDetector.OnDoubleTapListener {
@Override
public boolean onDoubleTap(final MotionEvent e) {
for (int i = mOverlays.size() - 1; i >= 0; i--)
if (mOverlays.get(i).onDoubleTapUp(e, MapView.this))
return true;
final GeoPoint center = getProjection().fromPixels(e.getX(), e.getY());
return zoomInFixing(center);
}
我似乎无法超越它。我正在使用3.0.1 jar和相关的javadocs。我想知道Mapview的setTouchDelegate方法是否有用,但是javadocs中没有对它的引用。您有什么建议吗?我不确定您是从哪里调用redrawOverlay(),但是如果您查看MinimapOverlay,您将看到一个示例,其中在屏幕上的固定位置绘制了一些内容。换句话说,您是在屏幕坐标而不是地图坐标中绘制的 例如:
@Override
protected void onDraw(final Canvas pC, final MapView pOsmv) {
// Calculate the half-world size
final Rect viewportRect = new Rect();
final Projection projection = pOsmv.getProjection();
final int zoomLevel = projection.getZoomLevel();
final int tileZoom = projection.getTileMapZoom();
mWorldSize_2 = 1 << (zoomLevel + tileZoom - 1);
// Find what's on the screen
final BoundingBoxE6 boundingBox = projection.getBoundingBox();
final Point upperLeft = org.osmdroid.views.util.Mercator
.projectGeoPoint(boundingBox.getLatNorthE6(), boundingBox.getLonWestE6(),
zoomLevel + tileZoom, null);
final Point lowerRight = org.osmdroid.views.util.Mercator
.projectGeoPoint(boundingBox.getLatSouthE6(), boundingBox.getLonEastE6(), zoomLevel
+ tileZoom, null);
// Save the Mercator coordinates of what is on the screen
viewportRect.set(upperLeft.x, upperLeft.y, lowerRight.x, lowerRight.y);
// Offset into OSM coordinates
viewportRect.offset(-mWorldSize_2, -mWorldSize_2);
// Draw a line from one corner to the other
canvas.drawLine(viewportRect.left, viewportRect.top, viewportRect.right, viewportRect.bottom);
@覆盖
受保护的void onDraw(最终画布pC、最终地图视图pOsmv){
//计算半个世界的大小
final Rect viewportRect=new Rect();
最终投影=pOsmv.getProjection();
final int zoomLevel=projection.getZoomLevel();
final int tileZoom=projection.getTileMapZoom();
mWorldSize_2=1,它当然会在屏幕上画一条线,好吧,一旦我将mWorldSize_2定义为int并向.drawline添加了一个Paint参数(我猜传入的画布与Google Maps传入的画布完全不同)。然而,底层的MapView消失了,我只是在灰色背景上得到了一条线。感谢您的帮助,我正在缓慢地到达那里!罪魁祸首是ListoOverlays.clear();我需要清除所有这些,除了索引0处的一条。(这与谷歌地图不同).我现在的主要问题是,我已经做了很多工作,通过我编写的服务/远程界面获取线条的坐标和一些文字。这些是屏幕像素坐标,因为谷歌覆盖层就是这样希望的。我现在需要将它们全部转换为Osmdroid的OSM坐标。完成了!非常感谢。之后r我已经清醒过来,拿着纸和笔坐下来,我意识到我只需要写两行transformX,transformY例程。它现在看起来就像谷歌地图覆盖。我已经编辑了原始问题,包括另一个查询。希望你不要介意。我不认为SO的任何其他成员能够提供帮助,因为我可以看到你还在为OsmdroidFwiw编写代码,链接断开了。很多示例。我假设MinimapOverlay.java
的模拟现在是SampleWithMinimapItemizedoverlay.java
,但我还没有玩过。
@Override
protected void onDraw(final Canvas pC, final MapView pOsmv) {
// Calculate the half-world size
final Rect viewportRect = new Rect();
final Projection projection = pOsmv.getProjection();
final int zoomLevel = projection.getZoomLevel();
final int tileZoom = projection.getTileMapZoom();
mWorldSize_2 = 1 << (zoomLevel + tileZoom - 1);
// Find what's on the screen
final BoundingBoxE6 boundingBox = projection.getBoundingBox();
final Point upperLeft = org.osmdroid.views.util.Mercator
.projectGeoPoint(boundingBox.getLatNorthE6(), boundingBox.getLonWestE6(),
zoomLevel + tileZoom, null);
final Point lowerRight = org.osmdroid.views.util.Mercator
.projectGeoPoint(boundingBox.getLatSouthE6(), boundingBox.getLonEastE6(), zoomLevel
+ tileZoom, null);
// Save the Mercator coordinates of what is on the screen
viewportRect.set(upperLeft.x, upperLeft.y, lowerRight.x, lowerRight.y);
// Offset into OSM coordinates
viewportRect.offset(-mWorldSize_2, -mWorldSize_2);
// Draw a line from one corner to the other
canvas.drawLine(viewportRect.left, viewportRect.top, viewportRect.right, viewportRect.bottom);