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', + }, +}); 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();