反应本机地理编码显示错误未获取坐标



我正在使用 https://github.com/marlove/react-native-geocoding 来获取反应原生的坐标。

示例代码

`Geocoder.from("visakhapatnam,andhra pradesh,india")
.then(json => {
var location = json.results[0].geometry.location;
console.log("location",location)
})
.catch(error => console.warn("routeMap Err",error));`

我收到错误

{"code": 4, "message": "地理编码时服务器出错。收到的数据位于错误的"来源"字段中。查看它以获取更多信息。 , "来源": {"error_message": "您必须在 https://console.cloud.google.com/project/_/billing/enable 在 https://developers.google.com/maps/gmp-get-started 上了解详情"中启用 Google Cloud Project 结算功能", "结果": [], "状态": "REQUEST_DENIED"}}

要使用地理编码 API,您必须具有 API 密钥。API 密钥是唯一标识符,用于验证与项目关联的请求,以便使用和计费。

要获取 API 密钥,请执行以下操作:

  1. 访问 Google Cloud Platform Console。
  2. 单击项目下拉列表,然后选择或创建项目 您要添加 API 密钥。
  3. 单击菜单按钮,然后选择API 和服务>凭据。
  4. 在"凭据">
  5. 页上,单击">创建凭据> API 密钥"。接口 密钥创建对话框显示新创建的 API 密钥。
  6. 单击"关闭"。新的 API 密钥列在"凭据"页面上的"凭据"页上 接口密钥。 (请记住在生产中使用 API 密钥之前对其进行限制。

或者您需要尝试启用计费,例如错误消息。

  1. 转到 Google Cloud 中的"付款帐号管理"页面 平台控制台并登录。
  2. 选择"我的项目"选项卡以显示项目列表和 与每个项目关联的付款帐户。
  3. 在项目列表中,找到要执行的项目 重新启用付款,然后点击旁边的菜单 (more_vert(。
  4. 选择更改付款,然后选择您的目标付款帐户 要。

您是否提供了 API 密钥来像这样进行地理编码 Geocoder.init('your api key'(;使用有效的 API 密钥

我遇到了这个问题,因为我的谷歌云免费试用计划结束了。当我升级到Blaze计划时,错误停止发生。

试试这个:

import React, {useEffect} from 'react';
import Geocoder from 'react-native-geocoding';
import {keys} from '../env';

首先确保计费信息和地理编码 API 设置正确 并启用。然后设置代码以授予权限以避免将来 问题

const _getCurrentPosition = async () => {
Geocoder.init(keys.GOOGLE_MAP_APIKEY); // initialized with a valid API key
const status = await locationPermission(); // write the function
if (status === 'granted') {
const location = await getCurrentLocation(); // write the function
console.log('Location Permission granted!');
console.log({currentLoc: location});

Geocoder.from({
latitude: location.latitude,
longitude: location.longitude,
})
.then(addressJson => {
const _location = addressJson.results[0].formatted_address;
console.log('location_', _location);
})
.catch(error => console.warn(error));
//or for non-geometry positions (plain addresses)
//**IMPORTANT** make sure the address is valid to avoid errors
//one way to do this is to first google a valid address
Geocoder.from("visakhapatnam")
.then(addressJson => {
const _location = addressJson.results[0].geometry.location;
console.log('location_', _location);
})
.catch(error => console.warn(error));
} else {
console.log('Permission not grantedd!');
}
};
useEffect(() => {
_getCurrentPosition();
}, []);

不要忘记始终查阅文档:https://www.npmjs.com/package/react-native-geocoding

相关内容

最新更新