-
|
I wasn't able to animate a marker even when I change the marker.coordinate. Am I doing it wrong? import { callback } from 'react-native-nitro-modules';
import { useCurrentLocation } from '@/hooks/location/useLocation';
// ... some constants
export default function MapView(props: Props) {
const { children, hideUserLocationMarker = false, ...rest } = props;
const { location: currentLoc } = useCurrentLocation(); // Custom implementation using expo-location
const initialLoc: RNLocation = useMemo(() => {
if (!currentLoc?.coords) return DEFAULT_LOCATION;
return {
center: {
latitude: currentLoc.coords.latitude,
longitude: currentLoc.coords.longitude,
},
altitude: currentLoc.coords.altitude || 0,
accuracy: currentLoc.coords.accuracy || 0,
bearing: currentLoc.coords.heading || 0,
speed: currentLoc.coords.speed || 0,
time: currentLoc.timestamp || 0,
};
}, [currentLoc]);
const initialProps: RNInitialProps = useMemo(
() => ({
camera: {
center: initialLoc.center,
zoom: 16,
},
}),
[initialLoc.center]
);
const [locationUpdates, setLocationUpdates] = useState<RNLocation>(initialLoc);
const userLocationMarker: RNMarker | null = useMemo(() => {
if (hideUserLocationMarker) return null;
return {
id: USER_MARKER_ID,
coordinate: locationUpdates.center,
title: 'You are here',
};
}, [hideUserLocationMarker, locationUpdates.center]);
const markers = useMemo(() => {
return userLocationMarker ? [userLocationMarker, ...(props.markers || [])] : props.markers;
}, [props.markers, userLocationMarker]);
return (
<Box className="flex-1">
<GoogleMapsView
// ... other props
markers={markers}
onLocationUpdate={callback((loc: RNLocation) => {
setLocationUpdates(loc);
})}
/>
{children}
//... spinner
</Box>
);
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
|
May I suggest adding import { callback } from 'react-native-nitro-modules';vs |
Beta Was this translation helpful? Give feedback.
-
|
@Jeius checkout the dev branch, there is a Markers screen with an example of a marker animation. |
Beta Was this translation helpful? Give feedback.
-
|
I’ve created a simple reusable hook for animating a LatLng coordinate using Reanimated — hopefully it’ll be useful to others too! import { useCallback, useState } from 'react';
import { type RNLatLng } from 'react-native-google-maps-plus';
import {
Easing,
SharedValue,
useAnimatedReaction,
useSharedValue,
withTiming,
type WithTimingConfig,
} from 'react-native-reanimated';
import { scheduleOnRN } from 'react-native-worklets';
export function useAnimatedLatLng(initialLatLng: RNLatLng) {
const animatedLat = useSharedValue(initialLatLng.latitude);
const animatedLng = useSharedValue(initialLatLng.longitude);
const [latlng, setLatLng] = useState<RNLatLng>(initialLatLng);
const animateTo = useCallback(
(
latlng: RNLatLng,
config: WithTimingConfig = { easing: Easing.inOut(Easing.ease), duration: 300 }
) => {
const animateValue = (sharedValue: SharedValue<number>, toValue: number) => {
if (!toValue) return;
sharedValue.value = withTiming(toValue, config);
};
animateValue(animatedLat, latlng.latitude);
animateValue(animatedLng, latlng.longitude);
},
[animatedLat, animatedLng]
);
useAnimatedReaction(
() => ({
latitude: animatedLat.value,
longitude: animatedLng.value,
}),
(current, prev) => {
if (current.latitude !== prev?.latitude || current.longitude !== prev.longitude) {
scheduleOnRN(setLatLng, current);
}
},
[]
);
return { animatedLatLng: latlng, animateTo };
}Usageimport { callback } from 'react-native-nitro-modules';
export default function MapView({ children, ...props }) {
const { animatedLatLng, animateTo } = useAnimatedLatLng(INITIAL_LATLNG);
const userLocationMarker: RNMarker = useMemo(() => ({
id: USER_MARKER_ID,
coordinate: animatedLatLng,
title: 'You are here',
}), [animatedLatLng]);
const markers = useMemo(() => [userLocationMarker, ...(props.markers || [])], [props.markers, userLocationMarker]);
return (
<View>
<GoogleMapsView
// ...rest of props...
hybridRef={callback((ref) => (props.mapRef.current = ref))}
markers={markers}
onLocationUpdate={callback(({ center }: RNLocation) => {
animateTo(center);
})}
/>
{children}
</View>
);
} |
Beta Was this translation helpful? Give feedback.
@Jeius checkout the dev branch, there is a Markers screen with an example of a marker animation.