Commit 7b0083cb9ff41815d679eb55ae7d8c51e3a6939c

Authored by 张志伟
1 parent cdc81874

新增poi搜索接口

lib/android/src/main/java/cn/feewee/amap3d/modules/AMapGeolocationModule.java
... ... @@ -6,26 +6,33 @@ import com.amap.api.location.AMapLocationClientOption;
6 6 import com.amap.api.location.AMapLocationListener;
7 7 import com.amap.api.services.core.AMapException;
8 8 import com.amap.api.services.core.LatLonPoint;
  9 +import com.amap.api.services.core.PoiItemV2;
9 10 import com.amap.api.services.geocoder.GeocodeSearch;
10 11 import com.amap.api.services.geocoder.RegeocodeAddress;
11 12 import com.amap.api.services.geocoder.RegeocodeQuery;
  13 +import com.amap.api.services.poisearch.PoiResultV2;
  14 +import com.amap.api.services.poisearch.PoiSearchV2;
12 15 import com.facebook.react.bridge.Arguments;
13 16 import com.facebook.react.bridge.Promise;
14 17 import com.facebook.react.bridge.ReactApplicationContext;
15 18 import com.facebook.react.bridge.ReactContextBaseJavaModule;
16 19 import com.facebook.react.bridge.ReactMethod;
17 20 import com.facebook.react.bridge.ReadableMap;
  21 +import com.facebook.react.bridge.WritableArray;
18 22 import com.facebook.react.bridge.WritableMap;
19 23 import com.facebook.react.modules.core.DeviceEventManagerModule;
20 24  
21 25 import org.jetbrains.annotations.NotNull;
22 26  
  27 +import java.util.ArrayList;
  28 +
23 29 @SuppressWarnings("unused")
24 30 public class AMapGeolocationModule extends ReactContextBaseJavaModule implements AMapLocationListener {
25 31 private final ReactApplicationContext reactContext;
26 32 private final AMapLocationClientOption option = new AMapLocationClientOption();
27 33 private DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter;
28 34 private AMapLocationClient client;
  35 + private GeocodeSearch geocodeSearch;
29 36  
30 37 public AMapGeolocationModule(ReactApplicationContext reactContext) {
31 38 super(reactContext);
... ... @@ -55,6 +62,7 @@ public class AMapGeolocationModule extends ReactContextBaseJavaModule implements
55 62 AMapLocationClient.updatePrivacyShow(reactContext, true, true);
56 63 AMapLocationClient.updatePrivacyAgree(reactContext, true);
57 64 client = new AMapLocationClient(reactContext);
  65 + geocodeSearch = new GeocodeSearch(reactContext);
58 66 client.setLocationListener(this);
59 67 eventEmitter = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
60 68 promise.resolve(null);
... ... @@ -210,6 +218,60 @@ public class AMapGeolocationModule extends ReactContextBaseJavaModule implements
210 218 }
211 219 }
212 220  
  221 + @ReactMethod
  222 + public void searchPoi(String keyWord, String city, Promise promise) {
  223 + WritableMap params = Arguments.createMap();
  224 + try {
  225 + if (city == null || city.isEmpty()) {
  226 + promise.reject(String.valueOf(-3), "请选择城市");
  227 + return;
  228 + }
  229 + if (keyWord == null || keyWord.isEmpty()) {
  230 + promise.reject(String.valueOf(-4), "请输入关键字");
  231 + return;
  232 + }
  233 + PoiSearchV2.Query query = new PoiSearchV2.Query(keyWord, "", city);
  234 + query.setDistanceSort(true);
  235 + query.setPageNum(1);
  236 + query.setPageSize(13);
  237 + PoiSearchV2 searchV2 = new PoiSearchV2(reactContext, query);
  238 + PoiResultV2 resultV2 = searchV2.searchPOI();
  239 + ArrayList<PoiItemV2> pois = resultV2.getPois();
  240 + WritableArray array = Arguments.createArray();
  241 + if (pois != null) {
  242 + for (PoiItemV2 info : pois) {
  243 + if (info.getLatLonPoint() == null || info.getTitle() == null ||
  244 + info.getTitle().isEmpty() || info.getSnippet() == null || info.getSnippet().isEmpty()) {
  245 + continue;
  246 + }
  247 + WritableMap par = Arguments.createMap();
  248 + par.putString("address", info.getSnippet());
  249 + par.putString("provinceCode", info.getProvinceCode());
  250 + par.putString("province", info.getProvinceName());
  251 + par.putString("cityCode", info.getCityCode());
  252 + par.putString("city", info.getCityName());
  253 + par.putString("areaCode", info.getAdCode());
  254 + par.putString("area", info.getAdName());
  255 + par.putString("name", info.getTitle());
  256 + par.putString("uid", info.getPoiId());
  257 + par.putString("typeCode", info.getTypeCode());
  258 + par.putString("type", info.getTypeDes());
  259 + par.putDouble("latitude", info.getLatLonPoint().getLatitude());
  260 + par.putDouble("longitude", info.getLatLonPoint().getLongitude());
  261 + array.pushMap(par);
  262 + }
  263 + params.putInt("total", array.size());
  264 + }
  265 + params.putArray("result", array);
  266 + promise.resolve(params);
  267 + } catch (AMapException ae) {
  268 + promise.reject(String.valueOf(ae.getErrorCode()), ae.getErrorMessage(), ae);
  269 + } catch (Exception e) {
  270 + promise.reject(String.valueOf(-1), "地址搜索失败,请重试", e);
  271 + }
  272 + }
  273 +
  274 +
213 275 private ReadableMap toJSON(AMapLocation location) {
214 276 if (location == null) {
215 277 return null;
... ...
lib/src/amap-geolocation.ts
1 1 import { NativeModules, NativeEventEmitter, Platform } from "react-native";
2   -import { ILocation as Location, ReGeocode, AppKey, LocationMode, LocationPurpose, GeoLanguage } from "./gen-types";
  2 +import {
  3 + ILocation as Location,
  4 + ReGeocode,
  5 + AppKey,
  6 + LocationMode,
  7 + LocationPurpose,
  8 + GeoLanguage,
  9 + PoiSearchResult,
  10 +} from "./gen-types";
3 11  
4 12 const AMapGeolocation = NativeModules.AMapGeolocation;
5 13 const eventEmitter = new NativeEventEmitter(AMapGeolocation);
... ... @@ -350,3 +358,21 @@ export function setLocatingWithReGeocode(withReGeocode: boolean) {
350 358 AMapGeolocation.setLocatingWithReGeocode(withReGeocode);
351 359 }
352 360 }
  361 +
  362 +/**
  363 + * 获取经纬度对应的位置描述
  364 + *
  365 + * @see https://lbs.amap.com/api/android-sdk/guide/map-data/geo#reverse-geocode
  366 + */
  367 +export function reverseGeoCode(lat = 0.0, lng = 0.0): Promise<ReGeocode> {
  368 + return AMapGeolocation.reverseGeoCode(lat, lng);
  369 +}
  370 +
  371 +/**
  372 + * 地点查询
  373 + *
  374 + * @see https://lbs.amap.com/demo/sdk/place-search#android
  375 + */
  376 +export function searchPoi(keyWord: string, city: string): Promise<PoiSearchResult> {
  377 + return AMapGeolocation.searchPoi(keyWord, city);
  378 +}
... ...
lib/src/gen-types.ts
... ... @@ -396,3 +396,43 @@ export interface ReGeocode {
396 396 */
397 397 poiName?: string;
398 398 }
  399 +
  400 +/**
  401 + * poi搜索结果
  402 + */
  403 +export interface PoiSearchResult {
  404 + total: number;
  405 + result: Poi[];
  406 +}
  407 +
  408 +/**
  409 + * poi信息
  410 + */
  411 +export interface Poi {
  412 + /** 地址 */
  413 + address: string;
  414 + /** 省 */
  415 + province: string;
  416 + /** 省code */
  417 + provinceCode: string;
  418 + /** 市 */
  419 + city: string;
  420 + /** 城市代码 */
  421 + cityCode: string;
  422 + /** 区 */
  423 + area: string;
  424 + /** 区域代码 */
  425 + areaCode: string;
  426 + /** poi名称 */
  427 + name: string;
  428 + /** id */
  429 + uid: string;
  430 + /** 类型码 */
  431 + typeCode: string;
  432 + /** 类型 */
  433 + type: string;
  434 + /** 纬度 */
  435 + latitude: number;
  436 + /** 经度 */
  437 + longitude: number;
  438 +}
... ...
package.json
1 1 {
2 2 "name": "@cqfw/react-native-amap3d",
3   - "version": "0.0.4",
  3 + "version": "0.0.7",
4 4 "description": "react-native 高德地图组件,支持 Android + iOS",
5 5 "author": "feewee",
6 6 "license": "MIT",
... ...