7

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

I have no problem displaying a completed route but am finding it difficult to show the track as I'm moving.

So far, I've found 2 different ways to do that but neither 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);
}

ie build up the polyline removing it before adding the new coordinates and then adding it back.

This is horrendously 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;
}

ie plot a series of single polylines.

Whilst this renders a lot faster than Method 1, it looks quite jagged, particularly at lower zoom levels because each polyline is squared off and it is only the corners that actually touch.

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

Doug Conran
  • 437
  • 1
  • 5
  • 17

1 Answers1

9

There's a straightforward solution using the 2.0 Maps API. You'll get a nice smooth route line using three steps:

  1. create a list of LatLng points such as:

    List<LatLng> routePoints;
    
  2. Add the route points to the list (could/should be done in a loop):

    routePoints.add(mapPoint);
    
  3. Create a Polyline and feed it the 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!

PeteH
  • 1,870
  • 25
  • 23
  • Thanks for the answer. I eventually came up with this myself (my method 1). The problem I was having was that when I iterated through an ArrayList of coordinates constructing and removing the polyline the processing was taking forever. However, when I offloaded that processing to the onChangeListener it worked like a charm. – Doug Conran Apr 17 '13 at 18:22
  • @PeteH Can you pleasse let me know, what is mapPoint in the method routePoints.add(mapPoint); – keshav kowshik May 14 '15 at 12:26
  • a mapPoint is simply an individual LatLng object, i.e., one of the desired points for your map display – PeteH May 23 '15 at 05:51