map.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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: 22.3193,
  33. longitude: 114.1694,
  34. latitudeDelta: 0.1,
  35. longitudeDelta: 0.1
  36. }}
  37. showsPointsOfInterest={false}
  38. showsUserLocation={true}
  39. showsBuildings={false}
  40. showsTraffic={false}
  41. style={styles.map}
  42. >
  43. <Marker
  44. coordinate={{ latitude: 22.3193, longitude: 114.1694 }}
  45. title="Marker Title"
  46. onPress={() => {}}
  47. description="Marker Description"
  48. />
  49. </MapView>
  50. )}
  51. </View>
  52. );
  53. }
  54. const styles = StyleSheet.create({
  55. container: {
  56. flex: 1
  57. },
  58. map: {
  59. width: '100%',
  60. height: '100%'
  61. }
  62. });