我应该使用React Native什么api来基于搜索获取地理位置

问题描述

我应该使用哪种API搜索特定位置并返回纬度和经度。我需要获取此信息,以便可以获取用户的位置。

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])
  }

响应为我提供了latlong,API也希望它返回latitudeDeltalongitudeDelta。响应将视口发回。视口坐标是否应该用于latitudeDeltalongitudeDelta

解决方法

要获取用户当前位置,您可以使用@react-native-community/geolocation库。

这样使用。

import Geolocation from '@react-native-community/geolocation';

Geolocation.getCurrentPosition(info => console.log(info));

您将收到类似this

的回复

在收到此回复后,您可以调用任何反向地理编码服务,例如google maps,LocationIq等。 我会向初学者建议locationIq免费。

例如将您从地理位置库获得的经度传递给此功能

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 => {
     
    });
};