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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#import "ClxMapPoiSearchPlugin.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
//#import <MAMapKit/MAGeometry.h>
@interface ClxMapPoiSearchPlugin ()<AMapSearchDelegate>
@property (nonatomic, strong) FlutterMethodChannel *channel;
@property (nonatomic, strong) AMapSearchAPI *search;
@property (nonatomic, strong) FlutterResult resultM;
@end
@implementation ClxMapPoiSearchPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"clx_map_poi_search"
binaryMessenger:[registrar messenger]];
ClxMapPoiSearchPlugin *instance = [[ClxMapPoiSearchPlugin alloc] init];
instance.channel = channel;
[registrar addMethodCallDelegate:instance channel:channel];
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
NSDictionary *arguments = call.arguments;
__weak ClxMapPoiSearchPlugin *weakSelf = self;
self.resultM = result;
if ([@"searchPOI#keywords" isEqualToString:call.method]) {
@try {
NSLog(@"---searchPOI#keywords--- arguments:%@", arguments);
if ([arguments.allKeys containsObject:@"keywords"]) {
NSString *keywords = arguments[@"keywords"];
NSLog(@"---handleMethodCall--- keywords:%@", keywords);
[weakSelf keywordsSearch:keywords];
}
} @catch(FlutterError *e) {
result(e);
}
} else if ([@"searchPOI#around" isEqualToString:call.method]) {
@try {
NSLog(@"---searchPOI#around--- arguments:%@", arguments);
if ([arguments.allKeys containsObject:@"latitude"] && [arguments.allKeys containsObject:@"longitude"]) {
NSString *latitude = arguments[@"latitude"];
NSString *longitude = arguments[@"longitude"];
[weakSelf aroundSearch:[latitude floatValue] Lng:[longitude floatValue]];
}
} @catch(FlutterError *e) {
result(e);
}
} else if ([@"searchPOI#initPoiSearch" isEqualToString:call.method]) {
@try {
NSDictionary *apiKey = arguments[@"apiKey"];
if (apiKey && [apiKey isKindOfClass:[NSDictionary class]]) {
NSString *iosKey = apiKey[@"iosKey"];
if (iosKey && iosKey.length > 0) {//通过flutter传入key,则再重新设置一次key
[AMapServices sharedServices].apiKey = iosKey;
NSLog(@"---handleMethodCall--- apiKey:%@", apiKey);
}
}
//这里统一检查key的设置是否生效
NSAssert(([AMapServices sharedServices].apiKey != nil), @"没有设置APIKey,请先设置key");
// 隐私合规
[AMapSearchAPI updatePrivacyShow:AMapPrivacyShowStatusDidShow
privacyInfo:AMapPrivacyInfoStatusDidContain];
[AMapSearchAPI updatePrivacyAgree:AMapPrivacyAgreeStatusDidAgree];
result(nil);
} @catch(FlutterError *e) {
result(e);
}
} else if ([@"searchPOI#aroundAll" isEqualToString:call.method]) {
@try {
NSLog(@"---searchPOI#aroundAll--- arguments:%@", arguments);
[weakSelf aroundSearchAll:arguments];
} @catch(FlutterError *e) {
result(e);
}
} else if ([@"searchPOI#getDistance" isEqualToString:call.method]) {
@try {
NSLog(@"---searchPOI#getDistance--- arguments:%@", arguments);
[weakSelf getDistance:arguments];
} @catch(FlutterError *e) {
result(e);
}
} else if ([@"getPlatformVersion" isEqualToString:call.method]) {
result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
} else {
result(FlutterMethodNotImplemented);
}
}
- (void)keywordsSearch:(NSString *)keywords {
AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
request.keywords = keywords;
request.cityLimit = YES;
request.page = 1;
request.offset = 20;
//是否返回扩展信息
//request.requireExtension = YES;
[self.search AMapPOIKeywordsSearch:request];
}
- (void)aroundSearch:(CGFloat)lat Lng:(CGFloat)lon {
AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
request.location = [AMapGeoPoint locationWithLatitude:lat longitude:lon];
request.page = 1;
request.offset = 20;
request.radius = 1000;
[self.search AMapPOIAroundSearch:request];
}
- (void)aroundSearchAll:(NSDictionary *)arguments {
NSNumber *latitude = arguments[@"latitude"];
NSNumber *longitude = arguments[@"longitude"];
NSString *keywords = arguments[@"keywords"];
NSString *city = arguments[@"city"];
if (![latitude isKindOfClass:[NSNull class]] && ![longitude isKindOfClass:[NSNull class]]) {
NSLog(@"aroundSearchAll 经纬度不为0 - latitude:%@, longitude:%@", latitude, longitude);
AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
request.location = [AMapGeoPoint locationWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
request.page = 1;
request.offset = 20;
request.radius = 1000;
if (![keywords isKindOfClass:[NSNull class]]) {
request.keywords = keywords;
}
if (![city isKindOfClass:[NSNull class]]) {
request.city = city;
}
[self.search AMapPOIAroundSearch:request];
} else {
NSLog(@"aroundSearchAll 经纬度为0 - latitude:%@, longitude:%@", latitude, longitude);
AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
if (![keywords isKindOfClass:[NSNull class]]) {
request.keywords = keywords;
}
if (![city isKindOfClass:[NSNull class]]) {
request.city = city;
}
request.cityLimit = YES;
request.page = 1;
request.offset = 20;
//是否返回扩展信息
//request.requireExtension = YES;
[self.search AMapPOIKeywordsSearch:request];
}
}
- (void)getDistance:(NSDictionary *)arguments {
// NSArray *pointF = arguments[@"pointF"];
// NSArray *pointT = arguments[@"pointT"];
//
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
// if (pointF.count == 2 && pointT.count == 2) {
// double latitudeF = [[pointF firstObject] doubleValue];
// double longitudeF = [[pointF lastObject] doubleValue];
//
// double latitudeT = [[pointT firstObject] doubleValue];
// double longitudeT = [[pointT lastObject] doubleValue];
//
// //1.将两个经纬度点转成投影点
// MAMapPoint point1 = MAMapPointForCoordinate(CLLocationCoordinate2DMake(latitudeF,longitudeF));
// MAMapPoint point2 = MAMapPointForCoordinate(CLLocationCoordinate2DMake(latitudeT,longitudeT));
// //2.计算距离
// CLLocationDistance distance = MAMetersBetweenMapPoints(point1, point2);
// NSNumber *numDistance = [NSNumber numberWithDouble:distance];
// [dict setValue:numDistance forKey:@"distance"];
//
// NSLog(@"getDistance: dict = %@", dict);
// self.resultM(dict);
// }
[dict setValue:@(0.0) forKey:@"distance"];
self.resultM(dict);
}
#pragma mark -
#pragma mark - AMapSearchDelegate
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response {
NSLog(@"onPOISearchDone 结束:%lu", (unsigned long)response.pois.count);
NSArray<AMapPOI *> *pois = response.pois;
if (pois.count > 0) {
NSMutableDictionary *arguments = [NSMutableDictionary dictionary];
NSMutableDictionary *data = [NSMutableDictionary dictionary];
NSMutableArray *listArray = [NSMutableArray array];
for (AMapPOI *poiItem in pois) {
NSMutableDictionary *dicMut = [NSMutableDictionary dictionary];
[dicMut setValue:@(poiItem.location.latitude) forKey:@"latitude"];
[dicMut setValue:@(poiItem.location.longitude) forKey:@"longitude"];
[dicMut setValue:poiItem.province forKey:@"provinceName"];
[dicMut setValue:poiItem.pcode forKey:@"provinceCode"];
[dicMut setValue:poiItem.city forKey:@"cityName"];
[dicMut setValue:poiItem.citycode forKey:@"cityCode"];
[dicMut setValue:poiItem.district forKey:@"adName"];
[dicMut setValue:poiItem.businessArea forKey:@"businessArea"];
[dicMut setValue:@(poiItem.distance) forKey:@"distance"];
[dicMut setValue:@"" forKey:@"snippet"];
[dicMut setValue:poiItem.name forKey:@"title"];
[listArray addObject:dicMut];
}
[data setValue:listArray forKey:@"poiList"];
[arguments setValue:data forKey:@"searchPOIResult"];
NSLog(@"onPOISearchDone 1 : arguments=%@", arguments);
// 回调
if (arguments) {
self.resultM(arguments);
//[_channel invokeMethod:@"camera#searchPOIEvent" arguments:arguments];
}
} else {
NSMutableDictionary *arguments = [NSMutableDictionary dictionary];
NSMutableDictionary *data = [NSMutableDictionary dictionary];
NSMutableArray *listArray = [NSMutableArray array];
[data setValue:listArray forKey:@"poiList"];
[arguments setValue:data forKey:@"searchPOIResult"];
self.resultM(arguments);
//[_channel invokeMethod:@"camera#searchPOIEvent" arguments:arguments];
NSLog(@"onPOISearchDone 2 : arguments=%@", arguments);
return;
}
}
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error {
NSLog(@"Error: %@", error);
}
#pragma mark -
#pragma mark - 懒加载
- (AMapSearchAPI *)search {
if (!_search) {
_search = [[AMapSearchAPI alloc] init];
_search.delegate = self;
}
return _search;
}
@end