react-nativegoogle-mapsgoogle-maps-api-3google-maps-markersmap-directions

How can I get Longitude and latitude of Address Enter in Input field in react native


I am showing google map Between two coordinate one is origin and other is Destination I want to convert input value into Longitude and latitude. Currently I set static values but I need to get longitude and latitude of values that Inter in input text

const [coordinates] = useState([
    {
      latitude: 51.5115,
      longitude: 10.1160,
    },
    {
      latitude:     51.521515,
      longitude: -0.127636,
    },
  ]);
  return(
 <View>


<MapView
       
       style={styles.map}
         initialRegion={{
           latitude: coordinates[0].latitude,
           longitude: coordinates[0].longitude,
           latitudeDelta: 2.0922,
           longitudeDelta: 2.0421,
         }}>
         <MapViewDirections
           origin={coordinates[0]}
           destination={coordinates[1]}
           apikey={"Google Api key"} 
           strokeWidth={4}
           optimizeWaypoints={true}
           strokeColor="red"
         />
         <Marker coordinate={coordinates[0]} />
         <Marker coordinate={coordinates[1]} />
       </MapView>

       <TextInput placeholder = "Enter Orgin " />

<TextInput   placeholder = "Enter Destination"  />  

 </View>
  )
   
  };

Solution

  • You can access the location with Expo as below code snippet.

    import * as Location from "expo-location";
    
    
    export const hasLocationServiceEnable = async () =>
      await Location.hasServicesEnabledAsync();
    
    const getUserLocation = async () => {
    
    
      let result = null;
    
      const isLocationAccessEnabled = await hasLocationServiceEnable();
    
      /** Discard request coordinates when user location disabled. This required to avoid
       * continuously request location in contrary to the user preference
       */
    
    
    
      if (!isLocationAccessEnabled) {
    
        return result;
      } else {
    
        // Request location access permission 
        let { status } = await Location.requestForegroundPermissionsAsync();
    
      
    
        if (status !== "granted") {
      
          return result;
        } else {
          try {
            let { coords } = await Location.getCurrentPositionAsync({});
    
           
            result = coords || null;
          } catch (error) {
    
            console.error(error)
           
          }
    
          return result;
        }
      }
    };
    
    export default getUserLocation;