Android 的 MapView 导航应用程序的性能问题
我有一个关于如何使导航应用程序更快、更稳定的问题。
我的应用程序的基本层是一个简单的地图视图,上面覆盖着多个叠加层(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您实际上什么时候要求线程运行?我只看到创建它的代码。如果这样做,您会发现只有主 (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.
您只能在 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.
我可能在这里大喊大叫,但我的猜测是,这与您在非 UI 线程的线程上对 MapView 进行更改这一事实有关。
我希望这会导致以下可能性之一:
希望这会有所帮助 - 至少为您指明了正确的方向。
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:
Hope this helps - at least by pointing you in vaguely the right direction.
人们希望您没有使用 Google MapView 来呈现导航,因为根据我对条款和条件的阅读,导航不包含在可接受的使用政策中。
摘自 Android Maps API 服务条款
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