ClxMapPoiSearchPlugin.m 10.0 KB
Newer Older
1 2 3
#import "ClxMapPoiSearchPlugin.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
4
//#import <MAMapKit/MAGeometry.h>
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

@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 {
31 32
            NSLog(@"---searchPOI#keywords--- arguments:%@", arguments);

33 34 35 36 37 38 39 40 41 42
            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 {
43 44
            NSLog(@"---searchPOI#around--- arguments:%@", arguments);

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
            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);
        }
75
    } else if ([@"searchPOI#aroundAll" isEqualToString:call.method]) {
76
        @try {
77
            NSLog(@"---searchPOI#aroundAll--- arguments:%@", arguments);
78 79 80 81
            [weakSelf aroundSearchAll:arguments];
        } @catch(FlutterError *e) {
            result(e);
        }
82 83 84 85 86 87 88
    } else if ([@"searchPOI#getDistance" isEqualToString:call.method]) {
        @try {
            NSLog(@"---searchPOI#getDistance--- arguments:%@", arguments);
            [weakSelf getDistance:arguments];
        } @catch(FlutterError *e) {
            result(e);
        }
89 90 91 92 93 94 95 96 97 98
    } 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;
99
    request.cityLimit = YES;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    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];
}

116
- (void)aroundSearchAll:(NSDictionary *)arguments {
117 118
    NSNumber *latitude = arguments[@"latitude"];
    NSNumber *longitude = arguments[@"longitude"];
119 120 121
    NSString *keywords = arguments[@"keywords"];
    NSString *city = arguments[@"city"];
    
122
    if (![latitude isKindOfClass:[NSNull class]] && ![longitude isKindOfClass:[NSNull class]]) {
123 124 125 126 127 128 129
        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;
130
        if (![keywords isKindOfClass:[NSNull class]]) {
131
            request.keywords = keywords;
132
        }
133
        if (![city isKindOfClass:[NSNull class]]) {
134
            request.city = city;
135 136 137 138 139 140
        }
        [self.search AMapPOIAroundSearch:request];
    } else {
        NSLog(@"aroundSearchAll 经纬度为0 - latitude:%@, longitude:%@", latitude, longitude);

        AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
141 142 143 144 145 146
        if (![keywords isKindOfClass:[NSNull class]]) {
            request.keywords = keywords;
        }
        if (![city isKindOfClass:[NSNull class]]) {
            request.city = city;
        }
147
        request.cityLimit = YES;
148 149 150 151 152
        request.page = 1;
        request.offset = 20;
        //是否返回扩展信息
        //request.requireExtension = YES;
        [self.search AMapPOIKeywordsSearch:request];
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
- (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);
}

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
#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"];
203
            [dicMut setValue:poiItem.district forKey:@"adName"];
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
            [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