map.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import React, { useEffect, useState } from 'react';
  2. import { StyleSheet, View } from 'react-native';
  3. import MapView, { Marker } from 'react-native-maps';
  4. import * as Location from 'expo-location';
  5. import { LocationObject } from 'expo-location';
  6. import { mapStyle } from '../../style/map';
  7. export default function Index() {
  8. /**********************************狀態管理**********************************/
  9. const [location, setLocation] = useState<LocationObject>();
  10. useEffect(() => {
  11. (async () => {
  12. let { status } = await Location.requestForegroundPermissionsAsync();
  13. if (status !== 'granted') {
  14. return;
  15. }
  16. let location = await Location.getCurrentPositionAsync({});
  17. setLocation(location);
  18. })();
  19. }, []);
  20. /**********************************狀態管理**********************************/
  21. /**********************************組件初始化**********************************/
  22. /**********************************組件初始化**********************************/
  23. /**********************************異步函數**********************************/
  24. /**********************************異步函數**********************************/
  25. return (
  26. <View>
  27. {location && (
  28. <MapView
  29. customMapStyle={mapStyle}
  30. followsUserLocation={true}
  31. initialRegion={{
  32. latitude: location.coords.latitude,
  33. longitude: location.coords.longitude,
  34. latitudeDelta: 0.01,
  35. longitudeDelta: 0.01
  36. }}
  37. showsUserLocation={true}
  38. showsBuildings={false}
  39. showsTraffic={false}
  40. style={styles.map}
  41. >
  42. <Marker
  43. coordinate={{ latitude: 22.3193, longitude: 114.1694 }}
  44. title="Marker Title"
  45. onPress={() => {}}
  46. description="Marker Description"
  47. />
  48. </MapView>
  49. )}
  50. </View>
  51. );
  52. }
  53. const styles = StyleSheet.create({
  54. container: {
  55. flex: 1
  56. },
  57. map: {
  58. width: '100%',
  59. height: '100%'
  60. }
  61. });