Skip to content Skip to sidebar Skip to footer

Google Map Api Making Lines Smoother When Bending

I am using Google Map API to get lines on the map in my application. I am loading the nodes of the lines from a database using following code: // Add polyline 'walks voda'

Solution 1:

You should not create a polyline for each two points, it should be a connected polyline with mulitple points, something like this:

publicvoiddrawRoute(List<LatLng> location) {
    polylineOptions = newPolylineOptions().width(MAPS_PATH_WIDTH).color(routeColor).addAll(location);
    polyLine = map.addPolyline(destinationRoutePolyLineOptions);
    polyLine.setPoints(location);
}

This will make it much smoother.

Solution 2:

Use the following code based on bSpline algorithm, it worked for me on Android.

public List<LatLng> bspline(List<LatLng> poly) {

    if (poly.get(0).latitude != poly.get(poly.size()-1).latitude || poly.get(0).longitude != poly.get(poly.size()-1).longitude){
        poly.add(new LatLng(poly.get(0).latitude,poly.get(0).longitude));
    }
    else{
        poly.remove(poly.size()-1);
    }
    poly.add(0,new LatLng(poly.get(poly.size()-1).latitude,poly.get(poly.size()-1).longitude));
    poly.add(new LatLng(poly.get(1).latitude,poly.get(1).longitude));

    Double[] lats = new Double[poly.size()];
    Double[] lons = new Double[poly.size()];

    for (int i=0;i<poly.size();i++){
        lats[i] = poly.get(i).latitude;
        lons[i] = poly.get(i).longitude;
    }

    double ax, ay, bx, by, cx, cy, dx, dy, lat, lon;
    float t;
    int i;
    List<LatLng> points = new ArrayList<>();
    // For every pointfor (i = 2; i < lats.length - 2; i++) {
        for (t = 0; t < 1; t += 0.2) {
            ax = (-lats[i - 2] + 3 * lats[i - 1] - 3 * lats[i] + lats[i + 1]) / 6;
            ay = (-lons[i - 2] + 3 * lons[i - 1] - 3 * lons[i] + lons[i + 1]) / 6;
            bx = (lats[i - 2] - 2 * lats[i - 1] + lats[i]) / 2;
            by = (lons[i - 2] - 2 * lons[i - 1] + lons[i]) / 2;
            cx = (-lats[i - 2] + lats[i]) / 2;
            cy = (-lons[i - 2] + lons[i]) / 2;
            dx = (lats[i - 2] + 4 * lats[i - 1] + lats[i]) / 6;
            dy = (lons[i - 2] + 4 * lons[i - 1] + lons[i]) / 6;
            lat = ax * Math.pow(t + 0.1, 3) + bx * Math.pow(t + 0.1, 2) + cx * (t + 0.1) + dx;
            lon = ay * Math.pow(t + 0.1, 3) + by * Math.pow(t + 0.1, 2) + cy * (t + 0.1) + dy;
            points.add(new LatLng(lat, lon));
        }
    }
    return points;

}

Post a Comment for "Google Map Api Making Lines Smoother When Bending"