简体   繁体   中英

Update direction path on Google Maps in driving mode

I am using Google Map, now I am showing a polyline b/w my current location to my destination by using Google Direction API with moving marker animation.

Now if I change my path while driving then how can I update that path from my current location to the destination.

Here is my code

@Override

public void onDirectionSuccess(Direction direction, String rawBody) {
    if (direction.isOK()) {

        route = direction.getRouteList().get(0);


        ArrayList<LatLng> directionPositionList = route.getLegList().get(0).getDirectionPoint();
        mMap.addPolyline(DirectionConverter.createPolyline(this, directionPositionList, 8, Color.BLUE));

    } else {
        Toast.makeText(DriversActivity.this, direction.getStatus(), Toast.LENGTH_SHORT).show();
    }

Here in OnDirectionSuccess method, m getting the direction. I don't want to call it again and again because the previous line is also there with a new one.

Is anyone can help me out???

please first add this function to activity

  private void route(AbstractRouting.TravelMode travelMode, final LatLng end) {
this.end = end;
DataShahrManager dataShahrManager = DataShahrManager.getInstance(activity);
if (dataShahrManager.getMyLocation() != null) {

  start = new LatLng(dataShahrManager.getMyLocation().getLatitude(),
      dataShahrManager.getMyLocation().getLongitude());
  if (end != null) {
    mapsFragment.getProgressBar().setVisibility(View.VISIBLE);
    Routing routing = new Routing.Builder()
        .travelMode(travelMode)
        .withListener(this)
        .alternativeRoutes(false)
        .waypoints(start, end)
        .language("fa")
        .key("your key")
        .build();
    routing.execute();
  }
} else {
  mapsFragment.getLocationProgressBar().setVisibility(View.VISIBLE);
  mapsFragment.startLocationFinding();
  mapsFragment.setUpMyLocationUsingMap();
  if (activity != null) {
    Toast.makeText(activity, R.string.finding_your_location, Toast.LENGTH_SHORT).show();
  }
  mapsFragment.setOnLocationFoundListener(() -> route(AbstractRouting.TravelMode.DRIVING, end));
}

} so add this line to button

      route(AbstractRouting.TravelMode.DRIVING, businessEntity.getLatLng());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM