Android draw a path on a mapView from a large amount of longitude/latitude points

家住魔仙堡 提交于 2019-12-01 06:49:16

问题


I am writing a application that needs to draw a "route" comprised of lots of GPS points (long+lat). The points are close together and don't follow roads, simply drawing a line between each point is ideal.

The current implementation I have is very slow as I am looping over all the GPS coordinates and creating a new Point and overlayitem in an itemized overlay. This takes around 20 seconds for it to load all of these points and draw them to the mapview. Is there a way in which I can construct a series of lines or point from the GPS coordinates and draw them onto the mapview?

Example of current implementation:

 for each set of long+lats // removed to simplify
    point= new GeoPoint(latitude,longitude);
    overlayitem = new OverlayItem(point,"","");
    itemizedOverlay.addOverlay(overlayitem);
 mapOverlays.add(itemizedOverlay);
 mMapController.setCenter(point);

Thanks in advance, hope I've explained it well enough.


回答1:


Check my reply with code sample here:

How to draw a path on a map using kml file?

This example parses a kml files (xml format as provided by Google Maps or Google Earth for route calculation) and draws the geo points onto the map. If you already have a list of geo points, you can just look at the drawPath() method; and adjust the way you pass the geo coords parameters to it (I encapsulated it into a simple bean that I named NavigationDataSet).




回答2:


It's not entirely clear to me what you're doing because I'm not sure how itemizedOverlay is drawing between the points. But I bet the primary problem is that you're using a bunch of OverlayItems when it'd be much faster to just skip that part entirely and override the itemizedOverlay's draw() method. I've done this before and it works quite well for line drawing. The basic pseudocode is:

create List of GeoPoints
add List to ItemizedOverlay
add a dummy marker to ItemizedOverlay (so that it knows to call `draw()`)
in ItemizedOverlay.draw(), use mapView.getProjection() to map the array of GeoPoints to x,y coords
use Canvas.drawLines() to draw a line between all your points

Since speed is a concern, make sure to create the Paint objects in the ItemizedOverlay's constructor; they can be reused as much as you want.

Also as a side note, it looks like you're using addOverlay() as demonstrated in the Hello, MapView demo. The only problem with using that is that they call populate() each time you add an item; if you are adding a bunch of points, it's better to add a bunch of overlays at once then call populate() at the end.



来源:https://stackoverflow.com/questions/3387894/android-draw-a-path-on-a-mapview-from-a-large-amount-of-longitude-latitude-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!