osmdroid-android-3.0.3.jar 中的覆盖

发布于 2024-10-29 08:56:04 字数 3123 浏览 2 评论 0原文

我之前曾成功使用 osmdroid-android-3.0.1.jar 将 OpenStreetMaps 合并到我的应用程序中,并在我的问题答案的大力帮助下

将 Google 地图应用移植到 Osmdroid - 叠加层问题

我现在已升级到使用 3.0.3 jar,但无法显示我的叠加层。

我的应用程序可以将显示从 Google 地图切换到 OSM,并在两者之上显示由线条和文本组成的相同叠加层。替代方案现在显示每个运行在自己的活动中,因为类相似但没有完全相同的方法,或者至少在 3.0.1 中没有。 (最终我想使用 Osmdroid 包装器 jar 将它们组合在一个活动中并减少重复的代码,但这是稍后的问题)现在我想获得与现在相同的功能使用新的 3.0.3 版本弃用了 3.0.1 jar。

使用新的 jar,我仍然可以正常显示地图视图,但覆盖层已经消失。我必须对代码进行一些更改,因为 3.0.1 onDraw() 方法现在已替换为 MapOverlay extends org.osmdroid.views.overlay.Overlay 中的 draw() (就像 Google 一样) 类。

前面的 onDraw() (现在绘制)方法中的所有代码都是从上面提到的问题的答案中逐字复制的,它工作得很好,尽管我承认没有完全理解世界大小、边界框和所描述的转换的概念。

我注意到该方法

final Point upperLeft = org.osmdroid.views.util.Mercator
            .projectGeoPoint(boundingBox.getLatNorthE6(), boundingBox.getLonWestE6(),
            zoomLevel + tileZoom, null);

现在已被弃用,我必须删除tileZoom 并

final int tileZoom = projection.getTileMapZoom();

让它进行编译。

当我到达 draw() 方法中的代码时,我可以在调试器中看到绘制叠加层所需的所有数据仍然存在且正确。绘制是通过canvas.drawLine(....)和canvas.drawText(....)等线条完成的。我根本没有使用函数的额外参数(布尔阴影)。

我的 redrawOverlay() 方法仍然是:

private void redrawOverlay() {

    mGpt = mMapVw.getMapCenter();

    if (mmapOverlay == null)
        mmapOverlay = new MapOverlay(this);
    mmapOverlay.setEnabled(true);
    List<Overlay> listOfOverlays = mMapVw.getOverlays();
    int ovlSize = listOfOverlays.size();
    if (ovlSize > 1)
        listOfOverlays.remove(1);
    listOfOverlays.add(mmapOverlay);
    mMapVw.invalidate();
}

(Google listofOverlays.clear() 不应该被使用,因为 osmdroid 3.0.1 将元素 0 作为地图本身 - 因此删除(1))

我想知道我必须做什么修改现有的 3.0.1 代码以适用于 3.0.3?我希望作者之一可能会读到这个问题。

更新

通过按照上述问题的答案中的建议调整 Minimap overaly,用于从左上角到右下角绘制叠加层的 draw() 方法现在变为:

    @Override
    protected void draw(Canvas pC,  MapView pOsmv, boolean shadow) {
        if(shadow)
            return;

        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);
        // Calculate the half-world size
          final Rect viewportRect = new Rect();
          final Projection projection = pOsmv.getProjection();
          final int zoomLevel = projection.getZoomLevel();
          int mWorldSize_2 = TileSystem.MapSize(zoomLevel) / 2;

          // Save the Mercator coordinates of what is on the screen
          viewportRect.set(projection.getScreenRect());
          // DON'T set offset with either of below
          //viewportRect.offset(-mWorldSize_2, -mWorldSize_2);
          //viewportRect.offset(mWorldSize_2, mWorldSize_2);

          // Draw a line from one corner to the other
          pC.drawLine(viewportRect.left, viewportRect.top, viewportRect.right, viewportRect.bottom, lp3);
    }

这可以正常工作

I'd previously managed to incorporate OpenStreetMaps into my application using osmdroid-android-3.0.1.jar with the great assistance of the answer to my question

Porting a Google Maps app to Osmdroid - problem with overlay

I've now upgraded to using the 3.0.3 jar and can't get my overlay to display.

My app can switch display from Google Maps to OSM and display the same overlay consisting of lines and texts on top of either. The alternative displays each run in their own activity for now, as the classes are similar but do not have absolutely identical methods, or at least they didn't have in 3.0.1. (Ultimately I'd like to use the Osmdroid wrapper jar to combine them in one activity and reduce the duplicate code, but that's a question for a later date) Right now I'd like to get the same functionality as I had with the now deprecated 3.0.1 jar using the new 3.0.3 version.

With the new jar, I still get the mapview displayed OK but the overlay has disappeared. I've had to make some changes to the code, as the 3.0.1 onDraw() method has now been replaced with draw() (just like Google) in the MapOverlay extends org.osmdroid.views.overlay.Overlay class.

All the code in the previous onDraw() (now draw) method has been copied verbatim from the answer to the question referred to above, It worked fine, although I confess to not fully understanding the concepts of worldsize, bounding boxes and the transformation described.

I notice that the method

final Point upperLeft = org.osmdroid.views.util.Mercator
            .projectGeoPoint(boundingBox.getLatNorthE6(), boundingBox.getLonWestE6(),
            zoomLevel + tileZoom, null);

is now deprecated, and I had to remove tileZoom and

final int tileZoom = projection.getTileMapZoom();

to get it to compile.

When I get to the code in the draw() method, I can see in the debugger that all the data necessary to draw the overlay is still present and correct. The drawing is done by lines such as canvas.drawLine(....) and canvas.drawText(....). I've not used the extra parameter to the function (boolean shadow) at all.

My redrawOverlay() method remains as:

private void redrawOverlay() {

    mGpt = mMapVw.getMapCenter();

    if (mmapOverlay == null)
        mmapOverlay = new MapOverlay(this);
    mmapOverlay.setEnabled(true);
    List<Overlay> listOfOverlays = mMapVw.getOverlays();
    int ovlSize = listOfOverlays.size();
    if (ovlSize > 1)
        listOfOverlays.remove(1);
    listOfOverlays.add(mmapOverlay);
    mMapVw.invalidate();
}

(The Google listofOverlays.clear() shouldn't be used as osmdroid 3.0.1 had element 0 as the map itself - hence the remove(1))

I'm wondering what I have to do to modify the existing 3.0.1 code to work with 3.0.3? I'm hoping that one of the authors of might read this question.

Update

By adapting the Minimap overaly as suggested in the answer to the question referred to above, the draw() method for drawing an overlay from top left to bottom right, now becomes:

    @Override
    protected void draw(Canvas pC,  MapView pOsmv, boolean shadow) {
        if(shadow)
            return;

        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);
        // Calculate the half-world size
          final Rect viewportRect = new Rect();
          final Projection projection = pOsmv.getProjection();
          final int zoomLevel = projection.getZoomLevel();
          int mWorldSize_2 = TileSystem.MapSize(zoomLevel) / 2;

          // Save the Mercator coordinates of what is on the screen
          viewportRect.set(projection.getScreenRect());
          // DON'T set offset with either of below
          //viewportRect.offset(-mWorldSize_2, -mWorldSize_2);
          //viewportRect.offset(mWorldSize_2, mWorldSize_2);

          // Draw a line from one corner to the other
          pC.drawLine(viewportRect.left, viewportRect.top, viewportRect.right, viewportRect.bottom, lp3);
    }

This works OK

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笙痞 2024-11-05 08:56:04

让我尝试提出几件事:

  1. 在新的jar构建中,调用getoverlays()。clear()将不再删除地图瓷砖覆盖层。呼叫删除(1)现在可能不正确,因为映射图块覆盖不再处于位置0。

  2. The projection has a method that will get you the screen coordinate rectangle of what is currently on the screen. You can check the coordinates you use to draw your lines with against this rectangle to make sure they intercept it.

  3. 看一下新的瓷砖系统类。可以使用这些功能重新创建任何被弃用的东西。

Let me try to suggest a few things:

  1. In the new jar build, calling getOverlays().clear() will no longer remove the map tile overlay. Calling remove(1) may be incorrect now since the map tile overlay is no longer in position 0.

  2. The projection has a method that will get you the screen coordinate rectangle of what is currently on the screen. You can check the coordinates you use to draw your lines with against this rectangle to make sure they intercept it.

  3. Take a look at the new TileSystem class. Anything that was deprecated can be recreated using these functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文