diff --git a/lib/android/build.gradle b/lib/android/build.gradle index 8959ed6..7d7c78f 100644 --- a/lib/android/build.gradle +++ b/lib/android/build.gradle @@ -40,5 +40,5 @@ dependencies { api 'com.facebook.react:react-native:+' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.amap.api:3dmap:9.4.0' - implementation 'com.amap.api:location:6.1.0' + implementation 'com.amap.api:search:9.4.0' } diff --git a/lib/android/src/main/java/cn/feewee/amap3d/modules/AMapGeolocationModule.java b/lib/android/src/main/java/cn/feewee/amap3d/modules/AMapGeolocationModule.java index 5eff063..7f417e8 100644 --- a/lib/android/src/main/java/cn/feewee/amap3d/modules/AMapGeolocationModule.java +++ b/lib/android/src/main/java/cn/feewee/amap3d/modules/AMapGeolocationModule.java @@ -4,8 +4,20 @@ import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationListener; -import com.facebook.react.bridge.*; +import com.amap.api.services.core.AMapException; +import com.amap.api.services.core.LatLonPoint; +import com.amap.api.services.geocoder.GeocodeSearch; +import com.amap.api.services.geocoder.RegeocodeAddress; +import com.amap.api.services.geocoder.RegeocodeQuery; +import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.Promise; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; +import com.facebook.react.bridge.ReadableMap; +import com.facebook.react.bridge.WritableMap; import com.facebook.react.modules.core.DeviceEventManagerModule; + import org.jetbrains.annotations.NotNull; @SuppressWarnings("unused") @@ -166,6 +178,38 @@ public class AMapGeolocationModule extends ReactContextBaseJavaModule implements client.setLocationOption(option); } + @ReactMethod + public void reverseGeoCode(double lat, double lng, Promise promise) { + if (lat <= 0 || lng <= 0) { + promise.reject(String.valueOf(-2), "无效的经纬度"); + return; + } + LatLonPoint poi = new LatLonPoint(lat, lng); + RegeocodeQuery regeocodeQuery = new RegeocodeQuery(poi, 100.0F, GeocodeSearch.AMAP); + try { + RegeocodeAddress fromLocation = geocodeSearch.getFromLocation(regeocodeQuery); + if (!fromLocation.getFormatAddress().isEmpty()) { + WritableMap map = Arguments.createMap(); + map.putString("address", fromLocation.getFormatAddress()); + map.putString("country", fromLocation.getCountry()); + map.putString("province", fromLocation.getProvince()); + map.putString("city", fromLocation.getCity()); + map.putString("cityCode", fromLocation.getCityCode()); + map.putString("district", fromLocation.getDistrict()); + map.putString("street", fromLocation.getStreetNumber().getStreet()); + map.putString("streetNumber", fromLocation.getStreetNumber().getStreet()); + map.putString("adCode", fromLocation.getAdCode()); + promise.resolve(map); + } else { + promise.reject(String.valueOf(-1), "坐标地址解析失败,请重试"); + } + } catch (AMapException ae) { + promise.reject(String.valueOf(ae.getErrorCode()), ae.getErrorMessage(), ae); + } catch (Exception e) { + promise.reject(String.valueOf(-1), "坐标地址解析失败,请重试", e); + } + } + private ReadableMap toJSON(AMapLocation location) { if (location == null) { return null; diff --git a/lib/src/sdk.ts b/lib/src/sdk.ts index 05290f5..db488f4 100644 --- a/lib/src/sdk.ts +++ b/lib/src/sdk.ts @@ -1,9 +1,10 @@ -import { NativeModules } from "react-native"; +import { NativeModules, Platform } from "react-native"; +import { AppKey } from "./gen-types"; const { AMapSdk } = NativeModules; -export function init(apiKey?: string) { - AMapSdk.initSDK(apiKey); +export function init(key: AppKey) { + AMapSdk.initSDK(Platform.select(key)); } export function getVersion(): Promise { diff --git a/package.json b/package.json index 36cf69e..a4d8eba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cqfw/react-native-amap3d", - "version": "0.0.3", + "version": "0.0.4", "description": "react-native 高德地图组件,支持 Android + iOS", "author": "feewee", "license": "MIT",