I’m trying to set a path from my location to my markers on the map with a click on the marker.
I have the location (the blue point) and the markers on the map: You can se here
I’ve tried to do it with the directions api but i was getting an error because of the billing(google not accepting pre-paid cards)
this is the code:
import React, { useEffect } from "react";
import {
StyleSheet,
Text,
TextInput,
View,
ScrollView,
Animated,
Image,
TouchableOpacity,
Dimensions,
Platform,
} from "react-native";
//theme
import { useTheme } from "@react-navigation/native";
//fast image
//import FastImage from 'react-native-fast-image'
//icons
import IonIcon from "react-native-vector-icons/Ionicons";
//map imports
import MapView, { Marker, PROVIDER_GOOGLE,Callout} from "react-native-maps";
import MapViewDirections from 'react-native-maps-directions';
import { markers, mapDarkStyle, mapStandardStyle } from "../model/mapData";
import StarRating from "../components/startRating";
//user location
import * as Location from 'expo-location';
const ExploreScreen = () => {
const theme = useTheme();
const initialMapState = {
markers,
region: {
//initial region location (mindelo)
latitude: 16.876546,
longitude: -24.98135,
latitudeDelta: 0.015,
longitudeDelta: 0.0121,
},
};
const [state, setState] = React.useState(initialMapState);
const _map = React.useRef(null);
//to get user location
const [location, setLocation] = React.useState(null);
const [errorMsg, setErrorMsg] = React.useState(null);
React.useEffect(() => {
(async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
let location = await Location.getCurrentPositionAsync({accuracy:Location.Accuracy.Highest, maximumAge: 10000}); //Accuracy to get the best location possible
setLocation(location);
})();
}, []);
return (
<View style={styles.container}>
<MapView
ref={_map}
initialRegion={state.region}
provider={PROVIDER_GOOGLE}
style={styles.map}
mapType="satellite"
showsUserLocation={true}
customMapStyle={theme.dark ? mapDarkStyle : mapStandardStyle}
>
{state.markers.map((marker, index) => {
return (
<Marker
optimizeWaypoints={true}
tracksViewChanges={false}
key={index}
coordinate={marker.coordinate}
// title={marker.title}
// description={marker.description}
onCalloutPress={() => alert('clicked')}
>
<Animated.View style={styles.content}>
<View style={styles.ballon}>
<Image source={marker.image} style={styles.imgEvent} resizeMode="cover"></Image>
</View>
</Animated.View>
<Callout tooltip= {true}>
<View style={[styles.card]}>
<Text>
<Image
style={styles.cardImage}
source={marker.image} />
</Text>
<View style={styles.textContent}>
<Text numberOfLines={1} style={styles.cardtitle}>{marker.title}</Text>
<StarRating rating={marker.rating} reviews={marker.reviews}></StarRating>
<Text numberOfLines={1} style={styles.cardDescription}>{marker.description}</Text>
<View style={styles.button}>
<TouchableOpacity style={[styles.signIn, { borderColor: '#FF6347' }, { borderWidth: 1 }]}>
<Text style={[styles.textSign, { color: "#FF6347" }]}>Comment about the event</Text>
</TouchableOpacity>
</View>
</View>
</View>
</Callout>
</Marker>
);
})}
</MapView>
{/* Search Box */}
<View style={[styles.searchBox, { borderRadius: 10 }, { padding: 7 }]}>
<TextInput
placeholder="Search here"
placeholderTextColor="#000"
autoCapitalize="none"
style={{ flex: 1, padding: 0 }}
></TextInput>
<IonIcon
name="ios-search"
size={20}
style={{ paddingTop: 3 }}
></IonIcon>
</View>
</View>
);
};
export default ExploreScreen;