123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
-
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title> src/components/Map.tsx</title>
- <script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
- <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
- <script src="./build/entry.js"></script>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <!--[if lt IE 9]>
- <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
- <![endif]-->
- <link href="https://fonts.googleapis.com/css?family=Roboto:100,400,700|Inconsolata,700" rel="stylesheet">
- <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
- <link type="text/css" rel="stylesheet" href="https://jmblog.github.io/color-themes-for-google-code-prettify/themes/tomorrow-night.min.css">
- <link type="text/css" rel="stylesheet" href="styles/app.min.css">
- <link type="text/css" rel="stylesheet" href="styles/iframe.css">
- <link type="text/css" rel="stylesheet" href="">
- <script async defer src="https://buttons.github.io/buttons.js"></script>
-
- </head>
- <body class="layout small-header">
- <div id="stickyNavbarOverlay"></div>
-
- <div class="top-nav">
- <div class="inner">
- <a id="hamburger" role="button" class="navbar-burger" aria-label="menu" aria-expanded="false">
- <span aria-hidden="true"></span>
- <span aria-hidden="true"></span>
- <span aria-hidden="true"></span>
- </a>
- <div class="logo">
-
-
- </div>
- <div class="menu">
-
- <div class="navigation">
- <a
- href="index.html"
- class="link"
- >
- API Documentation
- </a>
-
-
-
- </div>
- </div>
- </div>
- </div>
- <div id="main">
- <div
- class="sidebar "
- id="sidebarNav"
- >
-
- <nav>
-
- <h2><a href="index.html">Documentation</a></h2><div class="category"><h3>Interfaces</h3><ul><li><a href="AddLandmarkProps.html">AddLandmarkProps</a></li><li><a href="CommentProps.html">CommentProps</a></li><li><a href="IdToken.html">IdToken</a></li><li><a href="IntroProps.html">IntroProps</a></li><li><a href="Landmark.html">Landmark</a></li><li><a href="LandmarkDetailsProps.html">LandmarkDetailsProps</a></li><li><a href="LandmarkPinProps.html">LandmarkPinProps</a></li><li><a href="LMComment.html">LMComment</a></li><li><a href="ProfileProps.html">ProfileProps</a></li><li><a href="RegisterProps.html">RegisterProps</a></li><li><a href="UserProfile.html">UserProfile</a></li></ul><h3>Components</h3><ul><li><a href="App.html">App</a></li><li><a href="Atlas.html">Atlas</a></li><li><a href="Comment.html">Comment</a></li><li><a href="PrimaryButton.html">PrimaryButton</a></li><li><a href="Profile.html">Profile</a></li><li><a href="SecondaryButton.html">SecondaryButton</a></li></ul><h3>Global</h3><ul><li><a href="global.html#IconStrings">IconStrings</a></li></ul></div><div class="category"><h2>Hooks</h2><h3>Namespaces</h3><ul><li><a href="useAuth.html">useAuth</a></li><li><a href="useLandmarks.html">useLandmarks</a></li><li><a href="useProfile.html">useProfile</a></li></ul></div><div class="category"><h2>Map</h2><h3>Interfaces</h3><ul><li><a href="UserLocation.html">UserLocation</a></li></ul><h3>Components</h3><ul><li><a href="AddLandmark.html">AddLandmark</a></li><li><a href="LandmarkDetails.html">LandmarkDetails</a></li><li><a href="LandmarkPin.html">LandmarkPin</a></li><li><a href="Map.html">Map</a></li></ul></div><div class="category"><h2>Navigation</h2><h3>Components</h3><ul><li><a href="AuthorizedNavigator.html">AuthorizedNavigator</a></li><li><a href="UnauthorizedNavigator.html">UnauthorizedNavigator</a></li></ul><h3><a href="global.html">Global</a></h3></div><div class="category"><h2>Stores</h2><h3>Classes</h3><ul><li><a href="AuthStore.html">AuthStore</a></li></ul></div><div class="category"><h2>Unauthorized</h2><h3>Components</h3><ul><li><a href="Intro.html">Intro</a></li><li><a href="UnauthorizedLayout.html">UnauthorizedLayout</a></li></ul><h3>Components / Registration</h3><ul><li><a href="RegisterMain.html">RegisterMain</a></li></ul></div>
-
- </nav>
- </div>
- <div class="core" id="main-content-wrapper">
- <div class="content">
- <header class="page-title">
- <p>Source</p>
- <h1>src/components/Map.tsx</h1>
- </header>
-
-
- <section>
- <article>
- <pre class="prettyprint source linenums"><code>import { FontAwesome } from "@expo/vector-icons";
- import * as Location from 'expo-location';
- import React, { useEffect, useState } from "react";
- import { Keyboard, KeyboardAvoidingView, TouchableOpacity, View } from "react-native";
- import MapView, { LatLng, Region } from "react-native-maps";
- import Modal from "react-native-modal";
- import { colors } from "../globals";
- import { Landmark, useLandmarks } from "../hooks/useLandmarks";
- import AddLandmark from "./AddLandmark";
- import LandmarkDetails from "./LandmarkDetails";
- import LandmarkPin from "./LandmarkPin";
- /**
- * An interface representing the user location retrieved from [expo-location]{@link https://docs.expo.dev/versions/latest/sdk/location/}.
- * @category Map
- */
- export interface UserLocation {
- latitude: number;
- longitude: number;
- heading?: number;
- }
- const directionsUrl = "https://maps.googleapis.com/maps/api/directions/son?mode=walking&alternatives=true&key=AIzaSyD06YUMazlb4Fu0Q81y_YNyEBz8PmtZyeY";
- /**
- * The screen component containing the [react-native-maps]{@link https://github.com/react-native-maps/react-native-maps} Map and all related functionality.
- * @category Map
- * @component
- */
- export const Map: React.FC = () => {
- /**
- * State that contains the new {@link Landmark} object which is passed down to the {@link AddLandmark} modal.
- */
- const newLandmarkState: Landmark = {};
- const [newLandmark, setNewLandmark] = useState<Landmark>(newLandmarkState);
- /**
- * State that contains the selected {@link Landmark} object which is passed down to the {@link LandmarkDetails} modal.
- */
- const selectedLandmarkState: Landmark = {};
- const [selectedLandmark, setSelectedLandmark] = useState<Landmark>(selectedLandmarkState);
- /**
- * Holds the visibility state of the {@link AddLandmark} modal.
- */
- const lmAddVisibleState = false;
- const [lmAddVisible, toggleLmAdd] = useState<boolean>(false);
- /**
- * Holds the visibility state of the {@link LandmarkDetails} modal.
- */
- const lmDetailsVisibleState = false;
- const [lmDetailsVisible, toggleLmDetails] = useState<boolean>(false);
- /**
- * Flag that toggles whether or not editing is enabled in the {@link LandmarkDetails} modal.
- * The parent Map component has access to it so that it can disable closing the modal on backdrop press when it is enabled.
- */
- const lmDetailsEditingState = false;
- const [lmDetailsEditing, toggleLmDetailsEditing] = useState<boolean>(false);
- /**
- * State that holds a {@link UserLocation} object retrieved from location services.
- */
- const userLocationState: UserLocation | undefined = undefined;
- const [userLocation, setUserLocation] = useState<UserLocation>(userLocationState);
- /**
- * Flag that determines whether the map should focus and follow the user's location.
- */
- const followUserState = false;
- const [followUser, toggleFollowUser] = useState<boolean>(followUserState);
- //const [prevFetchedBounds, setFetchedBounds] = useState<Region>();
- const { landmarks, getLandmarksStatus, refetchLandmarks } = useLandmarks(undefined);
-
- /**
- * Ref that holds the loaded [MapView]{@link https://github.com/react-native-maps/react-native-maps/blob/master/docs/mapview.md} instance.
- */
- const mapRef = React.createRef<MapView>();
- useEffect(() => {
- /**
- * Prompts user to give permission to track their location using [expo-location]{@link https://docs.expo.dev/versions/latest/sdk/location/}.
- * If permission is granted, user location will be retrieved and stored in {@linkcode userLocationState}.
- * @memberOf Map
- */
- const requestLocationPermissions = async () => {
- let { status } = await Location.requestForegroundPermissionsAsync();
- if (status !== 'granted') {
- return;
- }
- const location = await Location.getCurrentPositionAsync();
- setUserLocation({latitude: location.coords.latitude, longitude: location.coords.longitude})
- // setFetchedBounds({
- // latitude: location.coords.latitude,
- // longitude: location.coords.longitude,
- // latitudeDelta: 0.01,
- // longitudeDelta: 0.01
- // });
- };
- requestLocationPermissions();
- }, []);
- /**
- * Triggered by long pressing on the map.
- * Sets {@linkcode newLandmarkState} to a skeleton {@link Landmark} object that only contains the pressed coordinates.
- * Triggers {@link openAddLandmark} via useEffect because the asyncronous nature of useState does not set the coordinates fast enough to toggle the modal directly through this method.
- */
- const promptAddLandmark = (coordinate: LatLng) => {
- setNewLandmark({latitude: coordinate.latitude, longitude: coordinate.longitude});
- }
- useEffect(() => {
- /**
- * Opens the {@link AddLandmark} modal when the user creates {@link newLandmarkState} by longpressing the map.
- * Embedded in a [useEffect]{@link https://reactjs.org/docs/hooks-effect.html} hook that listens to {@linkcode newLandmarkState}.
- * @memberOf Map
- */
- function openAddLandmark() {
- if (newLandmark) {
- toggleLmAdd(true)
- toggleLmDetails(false)
- console.log(newLandmark)
- }
- }
- openAddLandmark();
- }, [newLandmark]);
- // useEffect(() => {
- // if (prevFetchedBounds) {
- // refetchLandmarks();
- // }
- // }, [prevFetchedBounds]);
- /**
- * Triggered by on a {@link Landmark} displayed on the map.
- * Sets {@linkcode selectedLandmark} to the pressed {@link Landmark} object's value and toggles the {@link LandmarkDetails} modal.
- */
- const focusLandmark = (landmark: Landmark) => {
- setSelectedLandmark(landmark);
- toggleLmDetails(true)
- }
- /**
- * Animates the map to fly over to and focus on the user's location.
- */
- const flyToUser = () => {
- if (userLocation) {
- mapRef.current?.animateToRegion({latitude: userLocation.latitude, longitude: userLocation.longitude, latitudeDelta: 0.01, longitudeDelta: 0.01})
- }
- }
- // const checkBounds = (bounds: Region) => {
- // if (prevFetchedBounds) {
- // if (
- // bounds.latitude < prevFetchedBounds.latitude - prevFetchedBounds.latitudeDelta || // check if new lat exceeds old left bounds
- // bounds.latitude > prevFetchedBounds.latitude + prevFetchedBounds.latitudeDelta || // check if new lat exceeds old right bounds
- // bounds.longitude < prevFetchedBounds.longitude - prevFetchedBounds.longitudeDelta || // check if new lat exceeds bottom bounds
- // bounds.longitude > prevFetchedBounds.longitude + prevFetchedBounds.longitudeDelta ||
- // bounds.latitudeDelta < prevFetchedBounds.latitudeDelta / 2) // check if user zoomed in to atleast half scale of previous
- // {
- // console.log('new bounds')
- // setFetchedBounds(bounds);
- // }
- // }
- // else if (bounds.latitudeDelta < 5) {
- // console.log('initialize bounds')
- // setFetchedBounds(bounds);
- // }
- // }
- return (
- <View>
- <MapView
- testID="mapView"
- ref={mapRef}
- style={{width: "100%", height: "100%"}}
- initialRegion={userLocation ? {latitude: userLocation.latitude, longitude: userLocation.longitude, latitudeDelta: 0.01, longitudeDelta: 0.01} : undefined}
- onLongPress={e => promptAddLandmark(e.nativeEvent.coordinate)}
- // onRegionChangeComplete={bounds => checkBounds(bounds)}
- showsUserLocation={true}
- onUserLocationChange={e => setUserLocation(e.nativeEvent.coordinate)}
- followsUserLocation={followUser}>
- {landmarks?.map((landmark) => {
- return (
- <LandmarkPin key={landmark.id} landmark={landmark} focusLandmark={focusLandmark}/>)})}
- </MapView>
- <TouchableOpacity style={{position: 'absolute', bottom: 30, right: 30, backgroundColor: colors.red, height: 60, width: 60, borderRadius: 30, justifyContent: "center", alignItems: 'center'}} onPress={flyToUser}>
- <FontAwesome name="location-arrow" size={20} color="white"/>
- </TouchableOpacity>
- {/* <TouchableOpacity style={{position: 'absolute', bottom: 120, right: 30, backgroundColor: colors.red, height: 60, width: 60, borderRadius: 30, justifyContent: "center", alignItems: 'center'}}>
- <FontAwesome name="" size={20} color="white"/>
- </TouchableOpacity> */}
- <Modal
- testID="addLMModal"
- avoidKeyboard={true}
- onBackdropPress={() => toggleLmAdd(false)}
- style={{justifyContent: "flex-end", height: '100%', margin: 0}}
- isVisible={lmAddVisible} >
- <KeyboardAvoidingView>
- <AddLandmark setVisible={toggleLmAdd} landmark={newLandmark} />
- </KeyboardAvoidingView>
- </Modal>
- <Modal
- avoidKeyboard={true}
- onBackdropPress={() => {
- if (lmDetailsEditing) {
- Keyboard.dismiss();
- } else {
- toggleLmDetails(false)
- }
- }}
- style={{justifyContent: "flex-end", height: '100%', margin: 0}}
- isVisible={lmDetailsVisible}>
- <LandmarkDetails setVisible={toggleLmDetails} setEditing={toggleLmDetailsEditing} editingEnabled={lmDetailsEditing} landmark={selectedLandmark} />
- </Modal>
- </View> )
- }</code></pre>
- </article>
- </section>
- </div>
-
- <footer class="footer">
- <div class="content has-text-centered">
- <p>Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.7</a></p>
- <p class="sidebar-created-by">
- <a href="https://github.com/SoftwareBrothers/better-docs" target="_blank">BetterDocs theme</a> provided with <i class="fas fa-heart"></i> by
- <a href="http://softwarebrothers.co" target="_blank">SoftwareBrothers - JavaScript Development Agency</a>
- </p>
- </div>
- </footer>
-
- </div>
- <div id="side-nav" class="side-nav">
- </div>
- </div>
- <script src="scripts/app.min.js"></script>
- <script>PR.prettyPrint();</script>
- <script src="scripts/linenumber.js"> </script>
- </body>
- </html>
|