Advertisement
heraldogama

Untitled

Oct 18th, 2020
2,253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useEffect, useState } from "react";
  2. import { StyleSheet, Text, View, Dimensions } from "react-native";
  3. import MapView, { Marker, Callout, PROVIDER_GOOGLE } from "react-native-maps";
  4. import mapMarker from "../images/map-marker.png";
  5. import { Feather } from "@expo/vector-icons";
  6. import { useNavigation } from "@react-navigation/native";
  7. import { RectButton } from "react-native-gesture-handler";
  8. import api from "../services/api";
  9.  
  10. interface Orphanage {
  11.   id: number;
  12.   name: string;
  13.   latitude: number;
  14.   longitude: number;
  15. }
  16.  
  17. export default function OrphanagesMap() {
  18.   const [orphanages, setOrphanages] = useState<Orphanage[]>([]);
  19.   const navigation = useNavigation();
  20.  
  21.   useEffect(() => {
  22.     api.get("orphanages").then((response) => {
  23.       setOrphanages(response.data);
  24.     });
  25.   }, []);
  26.  
  27.   function handleNavigateToOrphanageDetails() {
  28.     navigation.navigate("OrphanageDetails");
  29.   }
  30.   function handleNavigateToCreateOrphanage() {
  31.     navigation.navigate("SelectMapPosition");
  32.   }
  33.  
  34.   return (
  35.     <View style={styles.container}>
  36.       <MapView
  37.         provider={PROVIDER_GOOGLE}
  38.         style={styles.map}
  39.         initialRegion={{
  40.           latitude: -2.5335324,
  41.           longitude: -44.1815166,
  42.           latitudeDelta: 0.008,
  43.           longitudeDelta: 0.008,
  44.         }}
  45.       >
  46.         {orphanages.map((orphanage) => {
  47.           return (
  48.             <Marker
  49.               key={orphanage.id}
  50.               icon={mapMarker}
  51.               calloutAnchor={{
  52.                 x: 2.7,
  53.                 y: 0.8,
  54.               }}
  55.               coordinate={{
  56.                 latitude: orphanage.latitude,
  57.                 longitude: orphanage.longitude,
  58.               }}
  59.             >
  60.               <Callout tooltip onPress={handleNavigateToOrphanageDetails}>
  61.                 <View style={styles.calloutContainer}>
  62.                   <Text style={styles.calloutText}>{orphanage.name}</Text>
  63.                 </View>
  64.               </Callout>
  65.             </Marker>
  66.           );
  67.         })}
  68.       </MapView>
  69.       <View style={styles.footer}>
  70.         <Text style={styles.footerText}>2 orfanatos encontrados</Text>
  71.         <RectButton
  72.           style={styles.createOrphanageButton}
  73.           onPress={handleNavigateToCreateOrphanage}
  74.         >
  75.           <Feather name="plus" size={20} color="#fff" />
  76.         </RectButton>
  77.       </View>
  78.     </View>
  79.   );
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement