From 2a66a0bf9dcef78302847f2314cfea33bd71fbba Mon Sep 17 00:00:00 2001 From: Omar Delahoz Date: Thu, 25 Apr 2024 19:47:58 -0700 Subject: [PATCH 1/2] fix: map doesnt show user marker --- .../react-native-web-maps/src/hooks/use-user-location.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/react-native-web-maps/src/hooks/use-user-location.tsx b/packages/react-native-web-maps/src/hooks/use-user-location.tsx index 3e5475d..cb02a55 100644 --- a/packages/react-native-web-maps/src/hooks/use-user-location.tsx +++ b/packages/react-native-web-maps/src/hooks/use-user-location.tsx @@ -42,12 +42,14 @@ export function useUserLocation(options: UseUserLocationOptions) { useEffect(() => { if (permission?.granted && options.followUserLocation) { - Location.getCurrentPositionAsync().then(handleLocationChange); // Watch position Location.watchPositionAsync( { accuracy: Location.Accuracy.Balanced }, handleLocationChange ).then(setWatchPositionSubscription); + } else if (permission?.granted) { + // Set location + Location.getCurrentPositionAsync().then(handleLocationChange); } return () => watchPositionSubscription?.remove(); From 39e37b8e69a5947ea9271515a4ae1add1b9fe0cc Mon Sep 17 00:00:00 2001 From: Omar Delahoz Date: Thu, 25 Apr 2024 21:00:14 -0700 Subject: [PATCH 2/2] feat: adds shows my location button --- packages/react-native-web-maps/package.json | 1 + .../src/components/map-view.tsx | 29 +++++++++++ .../src/components/user-location-button.tsx | 48 +++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 packages/react-native-web-maps/src/components/user-location-button.tsx diff --git a/packages/react-native-web-maps/package.json b/packages/react-native-web-maps/package.json index fd1b530..1597d69 100644 --- a/packages/react-native-web-maps/package.json +++ b/packages/react-native-web-maps/package.json @@ -48,6 +48,7 @@ "@testing-library/react": "12.1.5", "@types/geojson": "^7946.0.8", "@types/react": "~18.0.27", + "@types/react-dom": "^18.3.0", "@types/react-native": "~0.70.6", "@types/supercluster": "^7.1.0", "jest": "^29.2.1", diff --git a/packages/react-native-web-maps/src/components/map-view.tsx b/packages/react-native-web-maps/src/components/map-view.tsx index 2a1855b..2cf02e5 100644 --- a/packages/react-native-web-maps/src/components/map-view.tsx +++ b/packages/react-native-web-maps/src/components/map-view.tsx @@ -29,6 +29,8 @@ import { import { useUserLocation } from '../hooks/use-user-location'; import { UserLocationMarker } from './user-location-marker'; import * as Location from 'expo-location'; +import { createRoot } from 'react-dom/client'; +import { CurrentLocationButton } from './user-location-button'; function _MapView(props: MapViewProps, ref: ForwardedRef>) { // State @@ -281,6 +283,33 @@ function _MapView(props: MapViewProps, ref: ForwardedRef>) { } }, [props.followsUserLocation, userLocation]); + const panToUserLocation = () => { + if (map && userLocation) { + map?.panTo({ + lat: userLocation.coords.latitude, + lng: userLocation.coords.longitude, + }); + map?.setZoom(15); + } + }; + + useEffect(() => { + if (props.showsMyLocationButton) { + const buttonDiv = document.createElement('div'); + const rootElem = createRoot(buttonDiv); + rootElem.render( + + ); + + map?.controls[google.maps.ControlPosition.RIGHT_BOTTOM]?.setAt( + 0, + buttonDiv + ); + } else { + map?.controls[google.maps.ControlPosition.RIGHT_BOTTOM]?.pop(); + } + }, [props.showsMyLocationButton, userLocation]); + const mapNode = useMemo( () => ( { + props.onPressCurrentLocation(); + }} + > + + + + + + + + + ); +} + +const styles = StyleSheet.create({ + locateBtn: { + display: 'flex', + width: 40, + height: 40, + margin: 10, + backgroundColor: '#fff', + padding: 6, + borderRadius: 3, + elevation: 1, + shadowColor: '#000', + shadowOpacity: 0.2, + shadowRadius: 6, + zIndex: 189, + alignItems: 'center', + }, +});