Reputation: 343
What API should I use to search a specific location and get returned the lat and long. I need to get this information so that I can get a user's location.
const searchLocation = async (location = searchText) => {
let searchedLocation = location.nativeEvent.text;
const response = await axios.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${searchedLocation}&key=${googleMapsApiKey}`);
console.log('Response: ', response.data.results[0])
}
The response gives me the lat
and long
the API is also expecting to get back the latitudeDelta
and longitudeDelta
. The response sends back the viewport. Is the viewport coordinates suppose to be used for latitudeDelta
and longitudeDelta
?
Upvotes: 0
Views: 445
Reputation: 1039
For getting user current location you can use @react-native-community/geolocation library.
use like this.
import Geolocation from '@react-native-community/geolocation';
Geolocation.getCurrentPosition(info => console.log(info));
after getting this response you can call any reverse geocoding service like google maps, LocationIq, etc. I will suggest locationIq its free for starters.
e.g pass latitude longitude you got from geolocation library to this function
const apiReverseLocation = (lat, lon) => {
const key = '<your-api-key-here>';
const api = `https://us1.locationiq.com/v1/reverse.php?key=${key}&lat=${lat}&lon=${lon}&format=json`;
const request = axios.get(api);
request
.then(res => {
})
.catch(err => {
});
};
Upvotes: 1