Afi*_*han 3 geolocation reverse-geocoding react-native
我试图在 react native 中获取我当前的位置,使用react-native-geolocation获取我所在位置的纬度和经度。现在我想在不使用 Google API 密钥的情况下将它们转换为位置的地址。
有没有办法在不使用 Google API 密钥的情况下将纬度经度转换为地址?
sam*_*ann 10
有很多方法可以在不使用 Google Maps API 的情况下将 lon/lat 转换为地址。搜索reverse geocoding api,你会发现一堆替代品。
几个月前,谷歌向我收取了反向地理编码 API 请求的费用。所以我切换到Here。他们有一个免费套餐,每月提供 25 万次请求,适用于我的应用程序。请参阅此处的文档:https : //developer.here.com/documentation/examples/rest/geocoder/reverse-geocode 这将为您提供非常详细的地址数据(与穆罕默德建议的 ip-api.com 不同)。
这是我用来调用 API 的包装函数:
function getAddressFromCoordinates({ latitude, longitude }) {
return new Promise((resolve) => {
const url = `https://reverse.geocoder.ls.hereapi.com/6.2/reversegeocode.json?apiKey=${HERE_API_KEY}&mode=retrieveAddresses&prox=${latitude},${longitude}`
fetch(url)
.then(res => res.json())
.then((resJson) => {
// the response had a deeply nested structure :/
if (resJson
&& resJson.Response
&& resJson.Response.View
&& resJson.Response.View[0]
&& resJson.Response.View[0].Result
&& resJson.Response.View[0].Result[0]) {
resolve(resJson.Response.View[0].Result[0].Location.Address.Label)
} else {
resolve()
}
})
.catch((e) => {
console.log('Error in getAddressFromCoordinates', e)
resolve()
})
})
}
Run Code Online (Sandbox Code Playgroud)
有很多替代方案,您可以搜索反向地理编码 API
通过使用 Google 地图键
const myApiKey="Key Received from Google map"
function getAddressFromCoordinates({latitude, longitude}) {
return new Promise((resolve, reject) => {
fetch(
'https://maps.googleapis.com/maps/api/geocode/json?address=' +
latitude +
',' +
longitude +
'&key=' +
myApiKey,
)
.then(response => response.json())
.then(responseJson => {
if (responseJson.status === 'OK') {
resolve(responseJson?.results?.[0]?.formatted_address);
} else {
reject('not found');
}
})
.catch(error => {
reject(error);
});
});
}
Run Code Online (Sandbox Code Playgroud)
通过使用此处平台键
https://developer.here.com/documentation/geocoder/dev_guide/topics/example-reverse-geocoding.html
他们有一个免费套餐,为我们提供25 万个请求/月的 免费配额
const HERE_API_KEY="Key Received from Here Plateform"
function getAddressFromCoordinates({latitude, longitude}) {
return new Promise((resolve, reject) => {
const url = `https://reverse.geocoder.ls.hereapi.com/6.2/reversegeocode.json?apiKey=${HERE_API_KEY}&mode=retrieveAddresses&prox=${latitude},${longitude}`
fetch(url)
.then(res => res.json())
.then((resJson) => {
if (resJson
&& resJson.Response
&& resJson.Response.View
&& resJson.Response.View[0]
&& resJson.Response.View[0].Result
&& resJson.Response.View[0].Result[0]) {
resolve(resJson.Response.View[0].Result[0].Location.Address.Label)
} else {
reject('not found')
}
})
.catch((e) => {
reject(e);
})
})
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7057 次 |
| 最近记录: |