update polyline according to the user moving android googleMaps v2

后端 未结 3 1470
忘掉有多难
忘掉有多难 2020-12-06 12:00

I have googleMap (v2) with polyline that presents a route between the user current location and the destination point.
Now, I want to update the polyline according to th

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 12:43

    *I have already done working on updating polyline path without removing the polyline. We can do this by changing the points of that polyline. Check below code.

    This is the logic of setting new points to polyline.

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

    Detail Explanation:

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

提交回复
热议问题