How to display a moving track on an Android device

I want to build my track using GPS on an Android device.

I have no problem displaying the completed route, but it’s hard for me to show the track when I move.

So far I have found two different ways to do this, but none of them are particularly satisfactory.

METHOD 1

PolylineOptions track = new PolylineOptions(); Polyline poly; while (moving) { Latlng coord = new LatLng(lat,lng); // from LocationListener track.add(coord); if (poly != null) { poly.remove(); } poly = map.addPolyline(track); } 

those. create a polyline by deleting it before adding new coordinates, and then adding it back.

It is terribly slow.

METHOD 2

 oldcoord = new LatLng(lat,lng);; while (moving) { PolylineOptions track = new PolylineOptions(); LatLng coord = new (LatLng(lat,lng); track.add(oldcoord); track.add(coord); map.addPolyline(track); oldcoord = coord; } 

those. build a series of single polylines.

Despite the fact that this makes it much faster than method 1, it looks rather jagged, especially at lower zoom levels, because each polyline is square, and these are only the angles that actually touch.

Is there a better way, and if so, what is it?

+6
source share
1 answer

There is a simple solution using API 2.0 Maps. You will get a nice smooth route line using three steps:

  • create a list of LatLng points, such as:

     List<LatLng> routePoints; 
  • Add waypoints to the list (can / should be done in a loop):

     routePoints.add(mapPoint); 
  • Create a polyline and send it a list of LatLng points as such:

     Polyline route = map.addPolyline(new PolylineOptions() .width(_strokeWidth) .color(_pathColor) .geodesic(true) .zIndex(z)); route.setPoints(routePoints); 

Give it a try!

+8
source

All Articles