Android 的 MapView 导航应用程序的性能问题

发布于 2024-09-05 21:57:56 字数 1001 浏览 2 评论 0原文

我有一个关于如何使导航应用程序更快、更稳定的问题。

我的应用程序的基本层是一个简单的地图视图,上面覆盖着多个叠加层(2 个标记用于起点和目的地,一个标记用于路线)。

我的想法是实现一个线程来显示路线,以便应用程序在计算更复杂的路线时不会挂起(就像现在一样)。

实现线程后,不再显示任何更新,也许您可​​以帮助我浏览一下下面的代码摘录:

    private class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location loc) {
        posUser = new GeoPoint((int) (loc.getLatitude() * 1E6), (int) (loc
                .getLongitude() * 1E6));

        new Thread(){
        public void run(){
            mapView.invalidate();

            // Erase old overlays
            mapView.getOverlays().clear();

            // Draw updated overlay elements and adjust basic map settings
            updateText();
            if(firstRefresh){
                adjustMap();
                firstRefresh = false;
            }
            getAndPaintRoute();
            drawMarkers();
        }
        };
    }

一些功能已总结为“drawMarkers()”或“updateText()”等方法。 ..(他们不需要更多的关注;-))

I've got a question on making an navigation app more faster and more stable.

The basic layer of my app is a simple mapview, covered with several overlays (2 markers for start and destination and one for the route).

My idea is to implement a thread to display the route, so that the app won't hang up during the calculation of a more complex route (like it does right now).

After implementing the thread there are no updates shows any more, maybe you can help me with a short glance at an excerpt of my code below:

    private class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location loc) {
        posUser = new GeoPoint((int) (loc.getLatitude() * 1E6), (int) (loc
                .getLongitude() * 1E6));

        new Thread(){
        public void run(){
            mapView.invalidate();

            // Erase old overlays
            mapView.getOverlays().clear();

            // Draw updated overlay elements and adjust basic map settings
            updateText();
            if(firstRefresh){
                adjustMap();
                firstRefresh = false;
            }
            getAndPaintRoute();
            drawMarkers();
        }
        };
    }

Some features have been summarized to a method like "drawMarkers()" or "updateText()"...(they don't need any more attention ;-))

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

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

发布评论

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

评论(4

面犯桃花 2024-09-12 21:57:56

您实际上什么时候要求线程运行?我只看到创建它的代码。如果这样做,您会发现只有主 (UI) 线程才允许更新,正如 RPond 指出的那样。

相反,分开你的工作并 通过以下方式将结果发布回主线程一个处理程序

When are you actually asking for the thread to run? I only see code for creating it. If you did, you'd discover that only the main (UI) thread is allowed to update, as RPond notes.

Instead, split off your work and post the results back to the main thread via a Handler.

二智少女 2024-09-12 21:57:56

您只能在 UI 线程上更新 UI,所以我认为这就是问题所在。请查看这篇关于线程的文章,以获取此问题的解决方案。

You can only update the UI on the UI thread so I think that is the problem. Check out this article on threading for solutions to this problem.

好菇凉咱不稀罕他 2024-09-12 21:57:56

我可能在这里大喊大叫,但我的猜测是,这与您在非 UI 线程的线程上对 MapView 进行更改这一事实有关。

我希望这会导致以下可能性之一:

  • 您的更改引发了您没有看到的异常(再次可能是因为它在另一个线程上)
  • 您的更改被忽略,因为它们是在错误的线程上进行的。
  • 地图正在更新,但您的 UI 线程不知道需要重新绘制地图。

希望这会有所帮助 - 至少为您指明了正确的方向。

I may be barking up the wrong tree here, but my guess is that it's something to do with the fact that you're making changes to the MapView on a thread that isn't the UI thread.

I'd expect this to result in one of these possibilities:

  • Your changes are throwing an exception that you're not seeing (again possibly because it's on another thread)
  • Your changes are being ignored because they're being made on the wrong thread.
  • The map's being updated but your UI thread doesn't know it needs to redraw the map.

Hope this helps - at least by pointing you in vaguely the right direction.

无声情话 2024-09-12 21:57:56

人们希望您没有使用 Google MapView 来呈现导航,因为根据我对条款和条件的阅读,导航不包含在可接受的使用政策中。

摘自 Android Maps API 服务条款

根据第 8 条,您不得(也不得允许任何其他人):

...

8.7。将服务或内容与任何产品、系统或应用程序一起使用,以实现 (a) 基于传感器位置输入的实时导航或路线引导(包括但不限于任何视觉或听觉逐向路线)指导);

...

One would hope you're not using the Google MapView for rendering your navigation, since from my reading of the terms and conditions, navigation is not included in the acceptable use policy.

From the Android Maps APIs Terms of Service

Under this Section 8, you must not (nor may you permit anyone else to):

...

8.7. use the Service or Content with any products, systems, or applications for or in connection with (a) real time navigation or route guidance based on position input from a sensor (including but not limited to any visual or audible turn-by-turn route guidance);

...

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