Google Map API makes smooth lines more flexible when bent

I am using the Google Map API to get rows on a map in my application. I am loading row nodes from a database using the following code:

// Add polyline "walks voda"        
    List<WalkLine> dbwalknodes = dbclass.queryWalksFromDatabase(this); // list of latlng

    for (int i = 0; i < dbwalknodes.size() - 1 ; i++) {
        WalkLine source = dbwalknodes.get(i);
        WalkLine destination = dbwalknodes.get(i+1);
        Polyline line = mMap.addPolyline(new PolylineOptions() 
            .add(new LatLng(source.getLat(), source.getLon()),
                    new LatLng(destination.getLat(), destination.getLon()))
            .width(16)
            .color(Color.parseColor("#1b9e77"))
            .geodesic(true));
        line.setZIndex(1000);
     }

      

Do you have any ideas on how to create lines smoother when curving than in the image below? Is it possible?

https://www.dropbox.com/s/6waic988mj90kdk/2014-10-22%2012.48.04.png?dl=0

+3


source to share


2 answers


You shouldn't create a polyline for each of the two points, it should be a polyline connected with mulitple points, something like this:

public void drawRoute(List<LatLng> location) {
    polylineOptions = new PolylineOptions().width(MAPS_PATH_WIDTH).color(routeColor).addAll(location);
    polyLine = map.addPolyline(destinationRoutePolyLineOptions);
    polyLine.setPoints(location);
}

      



This will make it look smoother.

+3


source


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 point
    for (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;

}

      

+1


source







All Articles