uiwjs / react-native-amap-geolocation

React Native 高德地图定位模块,支持 Android/iOS。
https://uiwjs.github.io/react-native-amap-geolocation/
MIT License
42 stars 5 forks source link

你好,我android按照如下配置后老式返回null,无法定位 #20

Open cctvmikeowen opened 3 years ago

cctvmikeowen commented 3 years ago
import { Platform } from 'react-native';
import AMapGeolocation from '@uiw/react-native-amap-geolocation';

let apiKey = '';

if (Platform.OS === 'ios') {
  apiKey = '用于 iOS 的 apiKey';
}
if (Platform.OS === 'android') {
  apiKey = '用于 Android 的 apiKey';
}

// 设置 高德地图 apiKey
AMapGeolocation.setApiKey(apiKey);
// iOS 指定所需的精度级别
AMapGeolocation.setDesiredAccuracy(3);
// Android 指定所需的精度级别,可选设置,默认 高精度定位模式
AMapGeolocation.setLocationMode(1);
// 定位是否返回逆地理信息
AMapGeolocation.setLocatingWithReGeocode(true);

async function getCurrentLocation(){
  try {
    const json = await AMapGeolocation.getCurrentLocation();
    console.log('json:', json);
  } catch (error) {
    console.log('error:', error);
  }
}

getCurrentLocation的json 老是null,请问要如何配置

jaywcjlove commented 3 years ago

注意事项

⚠️ 高德地图定位部分 API 需要真机调试和 Access WiFi Information 权限。

Android:需要正确的设置 apiKey,获取 Key 的方法
官方获取 Key方法:https://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key A. 使用 `keytool`(jdk自带工具)获取 `SHA1`,默认 测试版本 `keystore` 路径 `<项目名称>/android/app/debug.keystore` ```bash keytool -v -list -keystore keystore文件路径 ``` B. 获取 `PackageName`,获取路径 `<项目名称>/android/app/src/main/AndroidManifest.xml` C. 在高德地图账号中设置 `SHA1` 和 `PackageName`。 D. 按照上面步骤正确设置你的 `apiKey` 才会起作用。
Android:无法获取逆地理信息的问题,KEY鉴权失败 1. APK 当前签名文件的 SHA1 与[高德开放平台](https://console.amap.com/)中设置不一致 2. Android Studio 开发者请注意调整 `build.gradle` 文件的 `applicationId` 与 `packageName` 一致,如不一致将会导致鉴权失败。 3. 工程中的 `packageName` 与[高德开放平台](https://console.amap.com/)中 `packageName` 设置不一致 4. 通过 `SDK` 提供的 `setApiKey(String key);` 接口设置 `Key`,注意 `Key` 设置要在 `SDK` 业务初始化之前。

@cctvmikeowen 注意看文档

cctvmikeowen commented 3 years ago

是真机调试的,debug和release 的appkey都设置了,核对是正确的, 权限也加了,还是返回null,连定位的授权对话框都不会弹,rn 0.64,安装采用yarn add @uiw/react-native-amap-geolocation,请问是哪里不对吗?

cctvmikeowen commented 3 years ago

是真机调试的,debug和release 的appkey都设置了,核对是正确的, 权限也加了,还是返回null,连定位的授权对话框都不会弹,rn 0.64,安装采用yarn add @uiw/react-native-amap-geolocation,请问是哪里不对吗?

jaywcjlove commented 3 years ago

image

我没有办法帮你解决,我只能告诉你我的文档里面研究成果,按照这个核对。

cctvmikeowen commented 3 years ago

你好,好像android,的有个bug,参照基本用法,js端调用AMapGeolocation.getCurrentLocation,映射到java端, @ReactMethod public void getCurrentLocation(final Promise promise) { try { if (client == null) { promise.reject("-1", "尚未调用 setApiKey() 进行初始化"); return; } if (!client.isStarted()) { client.startLocation(); } // System.out.println(mLastAMapLocation); promise.resolve(toJSON(mLastAMapLocation)); client.stopLocation(); } catch (Exception e) { promise.reject("-110", e.getMessage()); } },此时mLastAMapLocation 是null的

jaywcjlove commented 3 years ago

https://github.com/uiwjs/react-native-amap-geolocation/blob/303b42d65051719bdb973818a0ef64f106941232/android/src/main/java/com/uiwjs/react/geolocation/RNAMapGeolocationModule.java#L349

SDK 监听没有接收到位置信息。

@cctvmikeowen