根据移动android googleMaps v2的用户更新折线

时间:2013-06-11 07:01:36

标签: android google-maps android-maps-v2

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

当位置发生变化但折线闪烁时,我尝试重绘整条折线。

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

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

3 个答案:

答案 0 :(得分:14)

版本3.1.36的唯一方法:

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

希望API能够在以后的版本中得到增强。

答案 1 :(得分:1)

*我已经完成了不删除折线的更新折线路径的工作。我们可以通过更改折线的点来实现。检查以下代码。

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

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

答案 2 :(得分:0)

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

//计算2点之间的距离

 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();