map.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. console.log("Permission denied");
  15. return;
  16. }
  17. let location = await Location.getCurrentPositionAsync({});
  18. setLocation(location);
  19. })();
  20. }, []);
  21. /**********************************狀態管理**********************************/
  22. /**********************************組件初始化**********************************/
  23. /**********************************組件初始化**********************************/
  24. /**********************************異步函數**********************************/
  25. /**********************************異步函數**********************************/
  26. return (
  27. <View>
  28. {location && (
  29. <MapView
  30. customMapStyle={mapStyle}
  31. followsUserLocation={true}
  32. initialRegion={{
  33. latitude: 22.3193,
  34. longitude: 114.1694,
  35. latitudeDelta: 0.1,
  36. longitudeDelta: 0.1,
  37. }}
  38. showsPointsOfInterest={false}
  39. showsUserLocation={true}
  40. showsBuildings={false}
  41. showsTraffic={false}
  42. style={styles.map}
  43. >
  44. <Marker
  45. coordinate={{ latitude: 22.3193, longitude: 114.1694 }}
  46. title="Marker Title"
  47. onPress={() => {}}
  48. description="Marker Description"
  49. />
  50. </MapView>
  51. )}
  52. </View>
  53. );
  54. }
  55. const styles = StyleSheet.create({
  56. container: {
  57. flex: 1,
  58. },
  59. map: {
  60. width: "100%",
  61. height: "100%",
  62. },
  63. });