Google Map API making lines smoother when bending

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-14 14:13:42

问题


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"        
    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 idea how to create the lines smoother while it bends than on the picture bellow? Is it possible?

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


回答1:


You should not create a polyline for each two points, it should be a connected polyline 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 much smoother.




回答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 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;

}


来源:https://stackoverflow.com/questions/26508383/google-map-api-making-lines-smoother-when-bending

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!