Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fitbounds and directionService is not updating markers on the map #1206

Open
ahmetkocabiyik opened this issue Dec 25, 2023 · 1 comment
Open

Comments

@ahmetkocabiyik
Copy link

When i click a marker to create direction route, markers are not updating properly. We are using react 18.2.0 but same codes are working like a charm on react 16.13.1. I don't understand what is the problem.

Screen.Recording.2023-12-25.at.18-58-29.mp4
function onMarkerClick(lat, lng, markerId){
        setActiveMarkerId(markerId);
        setPath(null);
        if(route) route.setMap(null);

        if(map && maps) {

            const directionsService = new maps.DirectionsService();
            const directionsDisplay = new maps.DirectionsRenderer();
            directionsService.route({
                origin: new maps.LatLng(projectLocation.lat, projectLocation.lng),
                destination: new maps.LatLng(lat, lng),
                travelMode: 'DRIVING'
            }, (response, status) => {
                if (status === 'OK') {

                    directionsDisplay.setDirections(response);
                    setRoute(new maps.Polyline({
                        path: [],
                        geodesic : true,
                        strokeColor: getComputedStyle(document.documentElement).getPropertyValue('--map-route-color').trim(),
                        strokeOpacity: 1.0,
                        strokeWeight: 5,
                        editable: false,
                        map: map
                    }));
                    setPath(response.routes[0].overview_path);

                    setDistance(parseFloat((response.routes[0].legs[0].distance.value/1000)).toFixed(1))
                    setDuration(parseInt((response.routes[0].legs[0].duration.value/60)).toFixed(0))

                } else {
                    window.alert('Directions request failed due to ' + status);
                }
            });

            var bounds = new maps.LatLngBounds();
            bounds.extend(new maps.LatLng(projectLocation.lat, projectLocation.lng));
            bounds.extend(new maps.LatLng(lat, lng));

            let padding = window.innerWidth * 0.1;
            map.fitBounds(bounds,padding);

        }

    }
@The-Code-Monkey
Copy link

Im coming up with this same issue, it used to work, i know this because i am rebuilding a site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants