【问题标题】:Google Map API making lines smoother when bendingGoogle Map API 在弯曲时使线条更平滑
【发布时间】:2014-12-17 22:59:21
【问题描述】:

我正在使用 Google Map API 在我的应用程序中获取地图上的线条。我正在使用以下代码从数据库中加载行的节点:

// 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);
     }

你知道如何在弯曲时创建比下图更平滑的线条吗?有可能吗?

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

【问题讨论】:

  • 丑陋的解决方案:将宽度更改为更少;)

标签: android google-maps google-maps-android-api-2 polyline


【解决方案1】:

你不应该为每两个点创建一条折线,它应该是一条有多个点的连接折线,像这样:

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

这样会更流畅。

【讨论】:

  • 我使用了数组列表和函数“addAll”,效果很好。但我必须首先在他的回答here
【解决方案2】:

使用以下基于 bSpline 算法的代码,它适用于我在 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;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    相关资源
    最近更新 更多