我想在我的 React Native 应用程序中的谷歌地图中显示附近的餐厅。当我在附近调用谷歌API搜索时,它会抛出状态:'ZERO_RESULTS'



这是我的代码,

const url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?' + 'location=' + latitude + ',' + longitude + '&radius=100&type=restaurant&key=<My API Key>';
fetch(url)
.then((response) => response.json())
.then((JsonResponse) => {
// console.error(JsonResponse)
console.log(JsonResponse)
})
.catch((error) => {
alert('error')
});

有人可以指导我如何做到这一点,我是 React Native 的新手。 谢谢

Google关于状态代码的文档如下:

ZERO_RESULTS指示搜索成功但返回 没有结果。如果搜索latlng在 远程位置。

这看起来只是 API 无法找到结果的问题,因为您正在寻找给定(可能远程(位置 100 米内的餐馆。

请注意,当location设置为-33.8670522, 151.195736(澳大利亚悉尼的一个点(时,API 会返回查询的结果:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=100&type=restaurant&key=KEY

在您的情况下,由于纬度和经度对应于您当前的位置,因此您可以做的是增加搜索radius

希望对您有所帮助!

最新更新