Commit cdc818741c798c35b520b08ba6a00cd8871f7adc
1 parent
af27e31d
新增经纬度逆解析接口
Showing
4 changed files
with
51 additions
and
6 deletions
lib/android/build.gradle
... | ... | @@ -40,5 +40,5 @@ dependencies { |
40 | 40 | api 'com.facebook.react:react-native:+' |
41 | 41 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" |
42 | 42 | implementation 'com.amap.api:3dmap:9.4.0' |
43 | - implementation 'com.amap.api:location:6.1.0' | |
43 | + implementation 'com.amap.api:search:9.4.0' | |
44 | 44 | } | ... | ... |
lib/android/src/main/java/cn/feewee/amap3d/modules/AMapGeolocationModule.java
... | ... | @@ -4,8 +4,20 @@ import com.amap.api.location.AMapLocation; |
4 | 4 | import com.amap.api.location.AMapLocationClient; |
5 | 5 | import com.amap.api.location.AMapLocationClientOption; |
6 | 6 | import com.amap.api.location.AMapLocationListener; |
7 | -import com.facebook.react.bridge.*; | |
7 | +import com.amap.api.services.core.AMapException; | |
8 | +import com.amap.api.services.core.LatLonPoint; | |
9 | +import com.amap.api.services.geocoder.GeocodeSearch; | |
10 | +import com.amap.api.services.geocoder.RegeocodeAddress; | |
11 | +import com.amap.api.services.geocoder.RegeocodeQuery; | |
12 | +import com.facebook.react.bridge.Arguments; | |
13 | +import com.facebook.react.bridge.Promise; | |
14 | +import com.facebook.react.bridge.ReactApplicationContext; | |
15 | +import com.facebook.react.bridge.ReactContextBaseJavaModule; | |
16 | +import com.facebook.react.bridge.ReactMethod; | |
17 | +import com.facebook.react.bridge.ReadableMap; | |
18 | +import com.facebook.react.bridge.WritableMap; | |
8 | 19 | import com.facebook.react.modules.core.DeviceEventManagerModule; |
20 | + | |
9 | 21 | import org.jetbrains.annotations.NotNull; |
10 | 22 | |
11 | 23 | @SuppressWarnings("unused") |
... | ... | @@ -166,6 +178,38 @@ public class AMapGeolocationModule extends ReactContextBaseJavaModule implements |
166 | 178 | client.setLocationOption(option); |
167 | 179 | } |
168 | 180 | |
181 | + @ReactMethod | |
182 | + public void reverseGeoCode(double lat, double lng, Promise promise) { | |
183 | + if (lat <= 0 || lng <= 0) { | |
184 | + promise.reject(String.valueOf(-2), "无效的经纬度"); | |
185 | + return; | |
186 | + } | |
187 | + LatLonPoint poi = new LatLonPoint(lat, lng); | |
188 | + RegeocodeQuery regeocodeQuery = new RegeocodeQuery(poi, 100.0F, GeocodeSearch.AMAP); | |
189 | + try { | |
190 | + RegeocodeAddress fromLocation = geocodeSearch.getFromLocation(regeocodeQuery); | |
191 | + if (!fromLocation.getFormatAddress().isEmpty()) { | |
192 | + WritableMap map = Arguments.createMap(); | |
193 | + map.putString("address", fromLocation.getFormatAddress()); | |
194 | + map.putString("country", fromLocation.getCountry()); | |
195 | + map.putString("province", fromLocation.getProvince()); | |
196 | + map.putString("city", fromLocation.getCity()); | |
197 | + map.putString("cityCode", fromLocation.getCityCode()); | |
198 | + map.putString("district", fromLocation.getDistrict()); | |
199 | + map.putString("street", fromLocation.getStreetNumber().getStreet()); | |
200 | + map.putString("streetNumber", fromLocation.getStreetNumber().getStreet()); | |
201 | + map.putString("adCode", fromLocation.getAdCode()); | |
202 | + promise.resolve(map); | |
203 | + } else { | |
204 | + promise.reject(String.valueOf(-1), "坐标地址解析失败,请重试"); | |
205 | + } | |
206 | + } catch (AMapException ae) { | |
207 | + promise.reject(String.valueOf(ae.getErrorCode()), ae.getErrorMessage(), ae); | |
208 | + } catch (Exception e) { | |
209 | + promise.reject(String.valueOf(-1), "坐标地址解析失败,请重试", e); | |
210 | + } | |
211 | + } | |
212 | + | |
169 | 213 | private ReadableMap toJSON(AMapLocation location) { |
170 | 214 | if (location == null) { |
171 | 215 | return null; | ... | ... |
lib/src/sdk.ts
1 | -import { NativeModules } from "react-native"; | |
1 | +import { NativeModules, Platform } from "react-native"; | |
2 | +import { AppKey } from "./gen-types"; | |
2 | 3 | |
3 | 4 | const { AMapSdk } = NativeModules; |
4 | 5 | |
5 | -export function init(apiKey?: string) { | |
6 | - AMapSdk.initSDK(apiKey); | |
6 | +export function init(key: AppKey) { | |
7 | + AMapSdk.initSDK(Platform.select(key)); | |
7 | 8 | } |
8 | 9 | |
9 | 10 | export function getVersion(): Promise<string> { | ... | ... |