2024-12-06 20:49:28 +00:00
|
|
|
import { StyleSheet } from 'react-native'
|
2024-12-16 17:18:56 +00:00
|
|
|
import MapLibreGL, { Camera, FillLayer, LineLayer, MapView, PointAnnotation, RasterLayer, RasterSource, ShapeSource, UserLocation, UserTrackingMode } from '@maplibre/maplibre-react-native'
|
2024-12-15 17:18:19 +00:00
|
|
|
import { FontAwesome5, MaterialIcons } from '@expo/vector-icons'
|
2024-12-02 19:11:31 +00:00
|
|
|
import { circle } from '@turf/circle'
|
2024-12-13 00:18:55 +00:00
|
|
|
import { useLastOwnLocation, useLastPlayerLocations } from '@/hooks/useLocation'
|
2024-12-16 17:18:56 +00:00
|
|
|
import React, { useMemo, useState } from 'react'
|
2024-12-13 00:18:55 +00:00
|
|
|
import { PlayerLocation } from '@/utils/features/location/locationSlice'
|
|
|
|
import { useGame } from '@/hooks/useGame'
|
2024-12-15 17:18:19 +00:00
|
|
|
import { FAB } from 'react-native-paper'
|
2024-12-02 19:11:31 +00:00
|
|
|
|
2024-12-06 20:49:28 +00:00
|
|
|
export default function Map() {
|
2024-12-15 17:18:19 +00:00
|
|
|
const [followUser, setFollowUser] = useState(true)
|
|
|
|
return (
|
|
|
|
<>
|
2024-12-16 17:18:56 +00:00
|
|
|
<MapComponent followUser={followUser} setFollowUser={setFollowUser} />
|
2024-12-15 17:18:19 +00:00
|
|
|
<FAB
|
|
|
|
style={{ position: 'absolute', right: 25, bottom: 25 }}
|
|
|
|
icon={(props) => <MaterialIcons name={followUser ? 'my-location' : 'location-searching'} {...props} />}
|
|
|
|
onPress={() => setFollowUser(followUser => !followUser)} />
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-12-16 17:18:56 +00:00
|
|
|
function MapComponent({ followUser, setFollowUser }: { followUser?: boolean, setFollowUser: React.Dispatch<React.SetStateAction<boolean>> }) {
|
2024-12-02 19:11:31 +00:00
|
|
|
MapLibreGL.setAccessToken(null)
|
2024-12-15 17:18:19 +00:00
|
|
|
const userLocation = useLastOwnLocation()
|
2024-12-02 19:11:31 +00:00
|
|
|
return (
|
|
|
|
<MapView
|
2024-12-09 17:29:48 +00:00
|
|
|
logoEnabled={false}
|
2024-12-13 00:25:56 +00:00
|
|
|
compassViewPosition={2}
|
2024-12-02 19:11:31 +00:00
|
|
|
style={styles.map}
|
|
|
|
styleURL="https://openmaptiles.geo.data.gouv.fr/styles/osm-bright/style.json">
|
2024-12-06 20:49:28 +00:00
|
|
|
{userLocation && <Camera
|
2024-12-15 17:18:19 +00:00
|
|
|
defaultSettings={{centerCoordinate: [userLocation?.coords.longitude, userLocation?.coords.latitude], zoomLevel: 15}}
|
|
|
|
followUserLocation={followUser}
|
2024-12-16 17:18:56 +00:00
|
|
|
followUserMode={UserTrackingMode.Follow}
|
|
|
|
followZoomLevel={13}
|
|
|
|
onUserTrackingModeChange={(event) => {
|
|
|
|
if (followUser && !event.nativeEvent.payload.followUserLocation)
|
|
|
|
setFollowUser(false)
|
|
|
|
}} />}
|
2024-12-13 00:18:55 +00:00
|
|
|
|
2024-12-02 19:11:31 +00:00
|
|
|
<RasterSource id="railwaymap-source" tileUrlTemplates={["https://a.tiles.openrailwaymap.org/standard/{z}/{x}/{y}.png"]}></RasterSource>
|
|
|
|
<RasterLayer id="railwaymap-layer" sourceID="railwaymap-source" style={{rasterOpacity: 0.7}} />
|
|
|
|
|
2024-12-13 00:18:55 +00:00
|
|
|
<UserLocation animated={true} renderMode="native" androidRenderMode="compass" showsUserHeadingIndicator={true} />
|
|
|
|
<PlayerLocationsMarkers />
|
2024-12-02 19:11:31 +00:00
|
|
|
</MapView>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-12-13 00:18:55 +00:00
|
|
|
|
|
|
|
function PlayerLocationsMarkers() {
|
|
|
|
const game = useGame()
|
|
|
|
const lastPlayerLocations = useLastPlayerLocations()
|
2024-12-13 00:25:56 +00:00
|
|
|
return lastPlayerLocations ? lastPlayerLocations
|
2024-12-13 00:18:55 +00:00
|
|
|
.filter(playerLoc => playerLoc.playerId !== game.playerId)
|
2024-12-13 00:25:56 +00:00
|
|
|
.map(playerLoc => <PlayerLocationMarker key={`player-${playerLoc.playerId}-loc`} playerLocation={playerLoc} />) : <></>
|
2024-12-13 00:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function PlayerLocationMarker({ playerLocation }: { playerLocation: PlayerLocation }) {
|
|
|
|
const accuracyCircle = useMemo(() => circle([playerLocation.longitude, playerLocation.latitude], playerLocation.accuracy, {steps: 64, units: 'meters'}), [playerLocation])
|
|
|
|
return <>
|
|
|
|
<ShapeSource
|
|
|
|
id={`accuracy-radius-${playerLocation.playerId}`}
|
|
|
|
shape={accuracyCircle} />
|
|
|
|
<FillLayer
|
|
|
|
id={`accuracy-radius-fill-${playerLocation.playerId}`}
|
|
|
|
sourceID={`accuracy-radius-${playerLocation.playerId}`}
|
|
|
|
style={{fillOpacity: 0.4, fillColor: 'pink'}}
|
|
|
|
aboveLayerID="railwaymap-layer" />
|
|
|
|
<LineLayer
|
|
|
|
id={`accuracy-radius-border-${playerLocation.playerId}`}
|
|
|
|
sourceID={`accuracy-radius-${playerLocation.playerId}`}
|
|
|
|
style={{lineOpacity: 0.4, lineColor: 'red'}}
|
|
|
|
aboveLayerID={`accuracy-radius-fill-${playerLocation.playerId}`} />
|
|
|
|
<PointAnnotation id={`player-location-marker-${playerLocation.playerId}`}
|
|
|
|
coordinate={[playerLocation.longitude, playerLocation.latitude]}>
|
|
|
|
<FontAwesome5 name="map-marker-alt" size={24} color="red" />
|
|
|
|
</PointAnnotation>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
|
2024-12-02 19:11:31 +00:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
map: {
|
|
|
|
flex: 1,
|
|
|
|
alignSelf: 'stretch',
|
|
|
|
}
|
|
|
|
})
|