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

Advanced Marker is far away from the cursor on the map in FullSize mode #648

Open
DidukhV opened this issue Dec 25, 2024 · 7 comments
Open
Labels
needs more info Item needs further input from the creator

Comments

@DidukhV
Copy link

DidukhV commented Dec 25, 2024

Description

How does it work, guys? Why is this happening? In the video you can see that the advanced marker is far away from the cursor, but actually it's not only the marker problem, the same problem also with street view draggable control. It's happening only when the map is in full size.

I WANT TO NOTE THAT MARKER COMPONENT WORKS FINE UNDER THE SAME CONDITIONS

https://www.loom.com/share/5d41d860f6ac470da23be40eba6bf0a4?sid=278e7ad7-083c-4301-80b0-ee977ede368d

Code (PS: APIProvider is used but in the outer component):

export function VisGoogleMap(props: VisGoogleMapProps) {
    const map = useMap(props.mapId);
    const [markerRef, marker] = useAdvancedMarkerRef();
    const geocoder = useGoogleGeocode();

    const [geolocation, setGeolocation] = useState<ExtendedGeolocation>(INITIAL_GEO);
    const [infoWindowShown, setInfoWindowShown] = useState(false);

    useLayoutEffect(() => {
        if (!map) {
            return;
        }
        map.setCenter({ lat: geolocation.latitude, lng: geolocation.longitude } as google.maps.LatLngLiteral);
    }, [geolocation, map]);

    // Used to trigger local state update thus re-rendering of the map and position of the marker
    useEffect(() => {
        if (!props.geolocation) {
            return;
        }
        setGeolocation(props.geolocation);
    }, [props.geolocation]);

    useEffect(() => {
        // WE DON'T NEED TO UNNECESSARY CALL GEOCODER IF THE PROPS GEOLOCATION EQUALS TO THE SET ONE,
        // BUT ONLY IF THE USER SELECTED ITS CURRENT LOCATION
        if (
            geocoder &&
            geolocation &&
            (props.geolocation !== geolocation ||
                (!props.geolocation.text && props.geolocation.locationOrigin === GeolocationOrigin.ReportingUserLocation))
        ) {
            // WHEN THE MARKER IS DRAGGED TO ANOTHER LOCATION IT IS USED TO DETERMINE THE ADDRESS TEXT
            geocoder.geocode({ location: { lat: geolocation.latitude, lng: geolocation.longitude } }).then(e => {
                if (e.results.length > 0) {
                    const formattedAddress = e.results[0].formatted_address;
                    props.onGeoChange({
                        text: formattedAddress,
                        latitude: geolocation.latitude,
                        longitude: geolocation.longitude,
                        locationOrigin: GeolocationOrigin.ManuallySelected
                    });
                }
            });
        }
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [geolocation]);

    const handleMarkerClick = useCallback(() => setInfoWindowShown(isShown => !isShown), []);
    const handleClose = useCallback(() => setInfoWindowShown(false), []);
    const handleDragEnd = useCallback((e: google.maps.MapMouseEvent) => {
        setGeolocation({
            latitude: e.latLng?.lat() ?? 0,
            longitude: e.latLng?.lng() ?? 0
        });
    }, []);

    return (
        <Map
            mapId={props.mapId}
            id={props.mapId}
            defaultCenter={{ lng: INITIAL_GEO.longitude, lat: INITIAL_GEO.latitude }}
            defaultZoom={props.defaultZoom}
            disableDefaultUI={props.disableDefaultControls}
            streetViewControl={false}
            mapTypeControl={false}
        >
            <AdvancedMarker
                ref={markerRef}
                draggable={true}
                position={{ lng: geolocation?.longitude ?? INITIAL_GEO.longitude, lat: geolocation?.latitude ?? INITIAL_GEO.latitude }}
                onDragEnd={handleDragEnd}
                onClick={handleMarkerClick}
            />
            {infoWindowShown && geolocation?.text && (
                <InfoWindow anchor={marker} onClose={handleClose}>
                    {geolocation.text}
                </InfoWindow>
            )}
        </Map>
    );

Steps to Reproduce

Use the code above and check the video, and you will be able to reproduce

Environment

  • Library version: Latest
  • Google maps version: weekly
  • Browser and Version: Chrome, Edge
  • OS: Windows

Logs

No response

@DidukhV DidukhV added the bug Something isn't working label Dec 25, 2024
@DidukhV DidukhV changed the title [Bug] [Bug] Marker is far away from the cursor in FullSize map mode Dec 25, 2024
@DidukhV
Copy link
Author

DidukhV commented Dec 25, 2024

@jezmck @aemkei @morgler @usefulthink I need answers asap please to decide whether to go with your library or not.

@DidukhV DidukhV changed the title [Bug] Marker is far away from the cursor in FullSize map mode [Bug] Advanced Marker is far away from the cursor in FullSize map mode Dec 25, 2024
@DidukhV DidukhV changed the title [Bug] Advanced Marker is far away from the cursor in FullSize map mode [Bug] Advanced Marker is far away from the cursor on the map in FullSize mode Dec 25, 2024
@morgler
Copy link
Contributor

morgler commented Dec 25, 2024

Hey @DidukhV thanks for reporting this issue. I'm with the family for Xmas right now and can't look into it in time.

I've also not been a core developer up until now - just using the library like you and fixing a bug along the way.

If you investigate further it would be a great idea to add a failing automated test for your example above. Might also help other devs investigate even faster.

In case you can't fix it in time for you, you can also move to the Google library directly. Fewer features, but might still have all you need.

@DidukhV
Copy link
Author

DidukhV commented Dec 25, 2024

Hey, thanks for the answer, but I don't have the time digging into library, would be nice if you guys could take a look into this one. Thanks in advance and Merry Christmas 🎊

@usefulthink
Copy link
Collaborator

What would help immensely (save me some time and make it a lot more likely I will find the time to look into it during my vacation) would be an example that I can just open and look at. You can use any of our examples as a starting point (there's a "Try in CodeSandbox" link in every example).

@DidukhV
Copy link
Author

DidukhV commented Dec 26, 2024 via email

@usefulthink
Copy link
Collaborator

That's what I'm saying :D

I can of course do that myself, but it would save me some time and effort if you provided your example in codesandbox instead.

@usefulthink
Copy link
Collaborator

@DidukhV I tried, and can't see the behavior seen in the video. The code you posted doesn't work that well for me since it omits some pieces of information (type-definitions, how is it used, css-styles, etc) and contains things that aren't related to the problem in question.

Can you please try to provide a minimal, reproducible example that shows this problem (preferably in CodeSandbox or similar)? That way I should be able to help you solve it.

@usefulthink usefulthink added needs more info Item needs further input from the creator and removed bug Something isn't working labels Jan 10, 2025
@usefulthink usefulthink changed the title [Bug] Advanced Marker is far away from the cursor on the map in FullSize mode Advanced Marker is far away from the cursor on the map in FullSize mode Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs more info Item needs further input from the creator
Projects
None yet
Development

No branches or pull requests

3 participants