16

我有带有折线的 googleMap (v2),它显示了用户当前位置和目的地点之间的路线。
现在,我想根据用户移动来更新折线。

当位置改变但多段线闪烁时,我试图重绘整个多段线。

我在 PolylineOptions 类中没有找到任何合适
的函数(函数 add() 只是添加顶点,而不是更新或删除)

你知道如何更新折线吗???
感谢您抽出时间。

4

3 回答 3

19

从版本 3.1.36 开始的唯一方法:

List<LatLng> points = polyline.getPoints();
points.add(newPoint);
polyline.setPoints(points);

希望该 API 将在以后的版本中得到增强。

于 2013-06-11T07:17:06.010 回答
2

*我已经完成了更新折线路径而不删除折线的工作。我们可以通过改变该折线的点来做到这一点。检查下面的代码。

这就是将新点设置为折线的逻辑。

/*Here the routes contain the points(latitude and longitude)*/
for (int i = 0; i < routes.size(); i++) {
            Route route = routes.get(i);
            if(polyline_path != null){
                polyline_path.setPoints(route.points);
            }
        }

详细说明:

private GoogleMap map_object;
private Marker marker_driver;
private Marker marker_drop_off;
private Polyline polyline_path;
private PolylineOptions polylineOptions_path;

...
...
...

/*HelperDirectionFinder is a class that I create to call google API and I used this 
  class to get directions routes*/

/*I have created Service, and I'm calling this lines below after 5 sec. to get the 
  updated routes from google API.*/

HelperDirectionFinder directionFinder = new HelperDirectionFinder(
            JobEndScreen.this, source, destinations);
    try {
        directionFinder.showDirection();
    } catch (UnsupportedEncodingException e) {
        HelperProgressDialog.closeDialog();
    }

...
...
...

@Override
public void onDirectionFinderStart() {
    if(polylineOptions_path == null){
        HelperProgressDialog.showDialog(getActivity(), "", getString(R.string.text_loading));
    }
}


/*This interface method is called after getting routes from google API.*/
/*Here the routes contains the list of path or routes returned by Google Api*/
@Override
public void onDirectionFinderSuccess(List<Route> routes) {
    HelperProgressDialog.closeDialog();


    /*When polylineOptions_path is null it means the polyline is not drawn.*/
    /*If the polylineOptions_path is not null it means the polyline is drawn on map*/
    if(polylineOptions_path == null){
        for (Route route : routes) {
            polylineOptions_path = new PolylineOptions().
                    geodesic(true).
                    color(ContextCompat.getColor(getActivity(), R.color.color_bg_gray_dark)).
                    width(10);

            for (int i = 0; i < route.points.size(); i++)
                polylineOptions_path.add(route.points.get(i));

            polyline_path = map_object.addPolyline(polylineOptions_path);
        }
    }
    else {
        for (int i = 0; i < routes.size(); i++) {
            Route route = routes.get(i);
            if(polyline_path != null){
                polyline_path.setPoints(route.points);
            }
        }
    }
}
于 2018-07-02T08:08:21.077 回答
1
//Declare on global
PolylineOptions polyOptions, polyOptions2;
Polyline polyline2;
List<LatLng> ltln;
private Double lat_decimal = 0.0,lng_decimal=0.0;



//initialize
ltln = new ArrayList<>();

//如果你正在使用路由库

compile 'com.github.jd-alexander:library:1.0.7'

 @Override
    public void onRoutingSuccess(ArrayList<Route> arrayList, int i) {

    //Add this code snippet on onRoutingSuccess  to store latlan list
        ltln = new ArrayList<>();
        System.out.println("-----arrayList------" + arrayList.get(0).getPoints());
        ltln = arrayList.get(0).getPoints();



        //  NOTE here already we draw polyLine 1

       }


 //below mentioned how to update polyline

   private void UpdatePoliline(){
    System.out.println("-------runnablePolyline-------"+ltln.size());
    try {
        if (ltln.size() > 0) {
            for (int i = 0; i < ltln.size(); i++) {
                ltln.remove(i);
                if (CalculationByDistance(ltln.get(i), new LatLng(lat_decimal, lng_decimal)) >= 10) {
                    break;
                }

            }
            polyOptions2 = new PolylineOptions();
            polyOptions2.color(getResources().getColor(R.color.app_color));
            polyOptions2.width(7);
            polyOptions2.addAll(ltln);


            if (polyline2 == null) {
                polyline2 = googleMap.addPolyline(polyOptions2);
                if (polyLines.size() > 0) {
                    for (Polyline poly : polyLines) {
                        poly.remove();
                    }
                }
                polyLines.add(polyline2);
                if (polyline != null) {
                    polyline.remove();
                    polyline = null;
                }
            } else {
                polyline = googleMap.addPolyline(polyOptions2);
                if (polyLines.size() > 0) {
                    for (Polyline poly : polyLines) {
                        poly.remove();
                    }
                }
                polyLines.add(polyline);
                if (polyline2 != null) {
                    polyline2.remove();
                    polyline2 = null;
                }
            }
            System.out.println("=====waypoints new===" + ltln);
        }
    } catch (IndexOutOfBoundsException e) {
        e.printStackTrace();
    }
}

// 计算两点之间的距离

 public float CalculationByDistance(LatLng StartP, LatLng EndP) {


    Location locationA = new Location("Source");
    locationA.setLatitude(StartP.latitude);
    locationA.setLongitude(StartP.longitude);

    Location locationB = new Location("Destination");
    locationB.setLatitude(EndP.latitude);
    locationB.setLongitude(EndP.longitude);



    float distance = locationA.distanceTo(locationB);


    return distance;
}

//重绘折线 车辆不在当前折线中特定距离

    if (ltln.size() > 0) {

        if (PolyUtil.isLocationOnPath(new LatLng(currentlat, currentLon), ltln, false, 60.0f) ){
            System.out.println("===tolarance===" + true);
        } else {
           //Redraw Polyline
        }
    }

   UpdatePoliline();
于 2018-09-11T12:40:58.053 回答