forked from react-native-component/react-native-smart-amap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRCTAMapModule.java
More file actions
142 lines (125 loc) · 5.84 KB
/
RCTAMapModule.java
File metadata and controls
142 lines (125 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.reactnativecomponent.amap;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.facebook.react.bridge.Arguments;
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 java.util.List;
public class RCTAMapModule extends ReactContextBaseJavaModule implements PoiSearch.OnPoiSearchListener{
ReactApplicationContext mContext;
private PoiSearch poiSearch;
public RCTAMapModule(ReactApplicationContext reactContext) {
super(reactContext);
mContext = reactContext;
PoiSearch poiSearch = new PoiSearch(mContext, null);
this.poiSearch = poiSearch;
}
@Override
public String getName() {
return "AMapModule";
}
@ReactMethod
public void setOptions(final int reactTag, final ReadableMap options){
mContext.getCurrentActivity().runOnUiThread(new Runnable() {
public void run() {
final RCTAMapView mapView = ((RCTAMapView) mContext.getCurrentActivity().findViewById(reactTag));
if(options.hasKey("centerCoordinate")) {
ReadableMap centerCoordinateMap = options.getMap("centerCoordinate");
mapView.setLatLng(new LatLng(centerCoordinateMap.getDouble("latitude"), centerCoordinateMap.getDouble("longitude")));
}
if(options.hasKey("zoomLevel")) {
double zoomLevel = options.getDouble("zoomLevel");
mapView.setZoomLevel(zoomLevel);
}
}
});
}
@ReactMethod
public void setCenterCoordinate(final int reactTag, final ReadableMap coordinate){
mContext.getCurrentActivity().runOnUiThread(new Runnable() {
public void run() {
final RCTAMapView mapView = ((RCTAMapView) mContext.getCurrentActivity().findViewById(reactTag));
mapView.setCenterLocation(coordinate.getDouble("latitude"), coordinate.getDouble("longitude"));
}
});
}
@ReactMethod
public void searchPoiByCenterCoordinate(ReadableMap params) {
String types = "";
if(params.hasKey("types")) {
types = params.getString("types");
}
String keywords = "";
if(params.hasKey("keywords")) {
keywords = params.getString("keywords");
}
PoiSearch.Query query = new PoiSearch.Query(keywords, types);
if(params.hasKey("offset")) {
int offset = params.getInt("offset");
query.setPageSize(offset);// 设置每页最多返回多少条poiitem
}
if(params.hasKey("page")) {
int page = params.getInt("page");
query.setPageNum(page);//设置查询页码
}
poiSearch.setQuery(query);
if(params.hasKey("coordinate")) {
ReadableMap coordinateMap = params.getMap("coordinate");
double latitude = coordinateMap.getDouble("latitude");
double longitude = coordinateMap.getDouble("longitude");
poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(latitude, longitude), 1000));//设置周边搜索的中心点以及半径
}
poiSearch.setOnPoiSearchListener(this);
poiSearch.searchPOIAsyn();
}
@Override
public void onPoiSearched(PoiResult result, int rCode) {
List<PoiItem> poiItems;
WritableMap dataMap = Arguments.createMap();
if (rCode == 1000) {
if (result != null && result.getQuery() != null) {// 搜索poi的结果
// 取得搜索到的poiitems有多少页
poiItems = result.getPois();// 取得第一页的poiitem数据,页数从数字0开始
WritableArray array = Arguments.createArray();
for (PoiItem poi : poiItems) {
WritableMap data = Arguments.createMap();
data.putString("uid", poi.getPoiId());
data.putString("name", poi.getTitle());
data.putString("type", poi.getTypeDes());
data.putDouble("Longitude", poi.getLatLonPoint().getLongitude());
data.putDouble("Latitude", poi.getLatLonPoint().getLatitude());
data.putString("address", poi.getSnippet());
data.putString("tel", poi.getTel());
data.putInt("distance", poi.getDistance());
data.putString("cityCode", poi.getCityCode());
data.putString("cityName", poi.getCityName());
data.putString("provinceCode", poi.getProvinceCode());
data.putString("provinceName", poi.getProvinceName());
data.putString("adCode", poi.getAdCode());
data.putString("adName", poi.getAdName());
array.pushMap(data);
}
dataMap.putArray("searchResultList", array);
}
}
else {
WritableMap error = Arguments.createMap();
error.putString("code", String.valueOf(rCode));
dataMap.putMap("error", error);
}
mContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("amap.onPOISearchDone", dataMap);
}
@Override
public void onPoiItemSearched(PoiItem poiItem, int i) {
}
}