react-google-maps-api icon indicating copy to clipboard operation
react-google-maps-api copied to clipboard

How to clear previous routes from Direction Service and Direction Renderer?

Open sahilnare opened this issue 3 years ago • 3 comments

My Environment

OS: Linux (ubuntu) Browser: Firefox Node --version: v17.9.0 React version: 18.2.0 @react-google-maps/api version: 2.12.1

In my web app, I have to constantly update the route to show the marker moving along a road

directions1

But after the marker has gone ahead, the DirectionRenderer shows the previous route too

directions2

Here's how I'm using the library:

image

image

image

Is there any way to clear the previous routes?

sahilnare avatar Aug 08 '22 21:08 sahilnare

Hello @sahilnare , I've been working with this package and faced the same issue. Actually, the problem is in react's re-rendering not in the package.

Main problem: No matter what you apply, When the first time component shows the direction line on map, it generates the route more than one time and overlaps them on each other. And when you pass null then it removes only one direction line and the other remains there.

How I figured out the problem: I noticed that when I try to clear out the map's direction line by passing null value to directionResponse, then, the line color gets a little brighter. Then, I inspected the DOM of the map and I noticed that re-rendering is generating the direction line div more than one time.

Solution that worked for me: I had build the react app by using npm run build and then ran the production build locally using serve package. Finally, the bug was gone!

You can go through my project for more clarity on how to change map routes: https://github.com/tj-webdev/cab-fare-predictor

tj-webdev avatar Sep 22 '22 18:09 tj-webdev

Hello @sahilnare , I've been working with this package and faced the same issue. Actually, the problem is in react's re-rendering not in the package.

Main problem: No matter what you apply, When the first time component shows the direction line on map, it generates the route more than one time and overlaps them on each other. And when you pass null then it removes only one direction line and the other remains there.

How I figured out the problem: I noticed that when I try to clear out the map's direction line by passing null value to directionResponse, then, the line color gets a little brighter. Then, I inspected the DOM of the map and I noticed that re-rendering is generating the direction line div more than one time.

Solution that worked for me: I had build the react app by using npm run build and then ran the production build locally using serve package. Finally, the bug was gone!

You can go through my project for more clarity on how to change map routes: https://github.com/tj-webdev/cab-fare-predictor

thank

KavinduGD avatar Nov 28 '23 21:11 KavinduGD