[GIS] Android Google Maps average polyline paths

androidgoogle mapsjava

I'm making an android app that maps my path through plotting polylines using the google map API. If I have several of these paths, it just looks like a bunch of paths plotted on top of each other. Is there a way, through the Google Maps API to "average" these paths out? I was starting to head down a complicated path of computing the headings (geography util) and using those headings to get nearby polylines with similar headings and then averaging out the lats and lons to draw a polyline that was the average of all of the similar polylines. Before I do that I was wondering if anyone knew of a better way and/or if I should be using polygons instead of polylines? Here's a visual of what I am trying to do (sorry I used paint):
enter image description here

Best Answer

I don't know of a function in the googlemap api which would do what you want but I would do it kinda like below. I know that there are of course better ways of doing this but it is a simple way and you don't need the whole heading and bearing calculations.
You have to use the GoogleMap.getProjection().getProjection().toScreenLocation(LatLng) on every visible LatLng Point.

List<List<Point>> PointList;
boolean close = false;
int distance = 10;

for(Point actual : PointList.get(0)) {
    for(Point check : PointList.get(1)) {
         if(check.x < actual.x + distance && check.x > actual.x - distance 
            && check.y < actual.y + distance && check.y > actual.y - distance) {
              close = true;
         } else {
              close = false;
              break;
         }
    }
    if(!close) {
        break;
    }
}
if(close) {
    PointList.remove(1);
    //draw a polyline with thicker stroke and the points from PointList.get(0)
}

This checks if every point in pointlist1 is within a square of a point from pointlist2 independent where the Polyline startet or which directions they went. If your Points are to close together think about a path simplification algorithm like the Ramer–Douglas–Peucker algorithm.