From 7b0083cb9ff41815d679eb55ae7d8c51e3a6939c Mon Sep 17 00:00:00 2001 From: Kurisu Date: Thu, 13 Oct 2022 18:57:17 +0800 Subject: [PATCH] 新增poi搜索接口 --- lib/android/src/main/java/cn/feewee/amap3d/modules/AMapGeolocationModule.java | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/src/amap-geolocation.ts | 28 +++++++++++++++++++++++++++- lib/src/gen-types.ts | 40 ++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 4 files changed, 130 insertions(+), 2 deletions(-) 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 7f417e8..716fd04 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 @@ -6,26 +6,33 @@ import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationListener; import com.amap.api.services.core.AMapException; import com.amap.api.services.core.LatLonPoint; +import com.amap.api.services.core.PoiItemV2; import com.amap.api.services.geocoder.GeocodeSearch; import com.amap.api.services.geocoder.RegeocodeAddress; import com.amap.api.services.geocoder.RegeocodeQuery; +import com.amap.api.services.poisearch.PoiResultV2; +import com.amap.api.services.poisearch.PoiSearchV2; 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.WritableArray; import com.facebook.react.bridge.WritableMap; import com.facebook.react.modules.core.DeviceEventManagerModule; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; + @SuppressWarnings("unused") public class AMapGeolocationModule extends ReactContextBaseJavaModule implements AMapLocationListener { private final ReactApplicationContext reactContext; private final AMapLocationClientOption option = new AMapLocationClientOption(); private DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter; private AMapLocationClient client; + private GeocodeSearch geocodeSearch; public AMapGeolocationModule(ReactApplicationContext reactContext) { super(reactContext); @@ -55,6 +62,7 @@ public class AMapGeolocationModule extends ReactContextBaseJavaModule implements AMapLocationClient.updatePrivacyShow(reactContext, true, true); AMapLocationClient.updatePrivacyAgree(reactContext, true); client = new AMapLocationClient(reactContext); + geocodeSearch = new GeocodeSearch(reactContext); client.setLocationListener(this); eventEmitter = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class); promise.resolve(null); @@ -210,6 +218,60 @@ public class AMapGeolocationModule extends ReactContextBaseJavaModule implements } } + @ReactMethod + public void searchPoi(String keyWord, String city, Promise promise) { + WritableMap params = Arguments.createMap(); + try { + if (city == null || city.isEmpty()) { + promise.reject(String.valueOf(-3), "请选择城市"); + return; + } + if (keyWord == null || keyWord.isEmpty()) { + promise.reject(String.valueOf(-4), "请输入关键字"); + return; + } + PoiSearchV2.Query query = new PoiSearchV2.Query(keyWord, "", city); + query.setDistanceSort(true); + query.setPageNum(1); + query.setPageSize(13); + PoiSearchV2 searchV2 = new PoiSearchV2(reactContext, query); + PoiResultV2 resultV2 = searchV2.searchPOI(); + ArrayList pois = resultV2.getPois(); + WritableArray array = Arguments.createArray(); + if (pois != null) { + for (PoiItemV2 info : pois) { + if (info.getLatLonPoint() == null || info.getTitle() == null || + info.getTitle().isEmpty() || info.getSnippet() == null || info.getSnippet().isEmpty()) { + continue; + } + WritableMap par = Arguments.createMap(); + par.putString("address", info.getSnippet()); + par.putString("provinceCode", info.getProvinceCode()); + par.putString("province", info.getProvinceName()); + par.putString("cityCode", info.getCityCode()); + par.putString("city", info.getCityName()); + par.putString("areaCode", info.getAdCode()); + par.putString("area", info.getAdName()); + par.putString("name", info.getTitle()); + par.putString("uid", info.getPoiId()); + par.putString("typeCode", info.getTypeCode()); + par.putString("type", info.getTypeDes()); + par.putDouble("latitude", info.getLatLonPoint().getLatitude()); + par.putDouble("longitude", info.getLatLonPoint().getLongitude()); + array.pushMap(par); + } + params.putInt("total", array.size()); + } + params.putArray("result", array); + promise.resolve(params); + } 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/amap-geolocation.ts b/lib/src/amap-geolocation.ts index 10dcf04..eb4a4d7 100644 --- a/lib/src/amap-geolocation.ts +++ b/lib/src/amap-geolocation.ts @@ -1,5 +1,13 @@ import { NativeModules, NativeEventEmitter, Platform } from "react-native"; -import { ILocation as Location, ReGeocode, AppKey, LocationMode, LocationPurpose, GeoLanguage } from "./gen-types"; +import { + ILocation as Location, + ReGeocode, + AppKey, + LocationMode, + LocationPurpose, + GeoLanguage, + PoiSearchResult, +} from "./gen-types"; const AMapGeolocation = NativeModules.AMapGeolocation; const eventEmitter = new NativeEventEmitter(AMapGeolocation); @@ -350,3 +358,21 @@ export function setLocatingWithReGeocode(withReGeocode: boolean) { AMapGeolocation.setLocatingWithReGeocode(withReGeocode); } } + +/** + * 获取经纬度对应的位置描述 + * + * @see https://lbs.amap.com/api/android-sdk/guide/map-data/geo#reverse-geocode + */ +export function reverseGeoCode(lat = 0.0, lng = 0.0): Promise { + return AMapGeolocation.reverseGeoCode(lat, lng); +} + +/** + * 地点查询 + * + * @see https://lbs.amap.com/demo/sdk/place-search#android + */ +export function searchPoi(keyWord: string, city: string): Promise { + return AMapGeolocation.searchPoi(keyWord, city); +} diff --git a/lib/src/gen-types.ts b/lib/src/gen-types.ts index b663845..53101b8 100644 --- a/lib/src/gen-types.ts +++ b/lib/src/gen-types.ts @@ -396,3 +396,43 @@ export interface ReGeocode { */ poiName?: string; } + +/** + * poi搜索结果 + */ +export interface PoiSearchResult { + total: number; + result: Poi[]; +} + +/** + * poi信息 + */ +export interface Poi { + /** 地址 */ + address: string; + /** 省 */ + province: string; + /** 省code */ + provinceCode: string; + /** 市 */ + city: string; + /** 城市代码 */ + cityCode: string; + /** 区 */ + area: string; + /** 区域代码 */ + areaCode: string; + /** poi名称 */ + name: string; + /** id */ + uid: string; + /** 类型码 */ + typeCode: string; + /** 类型 */ + type: string; + /** 纬度 */ + latitude: number; + /** 经度 */ + longitude: number; +} diff --git a/package.json b/package.json index a4d8eba..c71b60f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cqfw/react-native-amap3d", - "version": "0.0.4", + "version": "0.0.7", "description": "react-native 高德地图组件,支持 Android + iOS", "author": "feewee", "license": "MIT", -- libgit2 0.22.2