|
|
@@ -1,19 +1,33 @@
|
|
|
#import "AmapLocationPlugin.h"
|
|
|
|
|
|
+@interface AmapLocationPlugin()<AMapLocationManagerDelegate> {
|
|
|
+ AMapLocationManager* _aMapManager;
|
|
|
+ FlutterMethodChannel* _flutterChannel;
|
|
|
+}
|
|
|
+
|
|
|
+@end
|
|
|
|
|
|
-@implementation AmapLocationPlugin{
|
|
|
- AMapLocationManager* _manager;
|
|
|
- FlutterMethodChannel* channel;
|
|
|
+@implementation AmapLocationPlugin
|
|
|
+
|
|
|
+-(instancetype)initWithBinaryMessager:(NSObject<FlutterBinaryMessenger>*)messenger{
|
|
|
+ if(self == [super init]){
|
|
|
+ _flutterChannel = [FlutterMethodChannel
|
|
|
+ methodChannelWithName:@"amap_location"
|
|
|
+ binaryMessenger:messenger];
|
|
|
+
|
|
|
+ }
|
|
|
+ return self;
|
|
|
}
|
|
|
+
|
|
|
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
|
|
|
- FlutterMethodChannel* channel = [FlutterMethodChannel
|
|
|
- methodChannelWithName:@"amap_location"
|
|
|
- binaryMessenger:[registrar messenger]];
|
|
|
- AmapLocationPlugin* instance = [[AmapLocationPlugin alloc] init];
|
|
|
- instance.channel = channel;
|
|
|
- [registrar addMethodCallDelegate:instance channel:channel];
|
|
|
- [registrar addApplicationDelegate:instance];
|
|
|
+ AmapLocationPlugin* instance = [[AmapLocationPlugin alloc] initWithBinaryMessager:registrar.messenger];
|
|
|
|
|
|
+ [registrar addMethodCallDelegate:instance channel:[instance channel]];
|
|
|
+ [registrar addApplicationDelegate:instance];
|
|
|
+}
|
|
|
+
|
|
|
+-(FlutterMethodChannel *)channel{
|
|
|
+ return _flutterChannel;
|
|
|
}
|
|
|
|
|
|
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
|
|
|
@@ -37,35 +51,40 @@
|
|
|
NSNumber* locationMode = call.arguments[@"locationMode"];
|
|
|
|
|
|
NSLog(@"开始定位");
|
|
|
- _manager = [[AMapLocationManager alloc]init];
|
|
|
- _manager.delegate =self;
|
|
|
- _manager.detectRiskOfFakeLocation=mockEnable.boolValue;
|
|
|
+ _aMapManager = [[AMapLocationManager alloc]init];
|
|
|
+ _aMapManager.delegate =self;
|
|
|
+ _aMapManager.detectRiskOfFakeLocation=mockEnable.boolValue;
|
|
|
|
|
|
- [_manager setLocatingWithReGeocode:isNeedAddress.boolValue];
|
|
|
- _manager.locationTimeout = locationTimeOut.intValue;
|
|
|
+ [_aMapManager setLocatingWithReGeocode:isNeedAddress.boolValue];
|
|
|
+ // 定位超时时间,最低2s,此处设置为10s
|
|
|
+ _aMapManager.locationTimeout = locationTimeOut.intValue;
|
|
|
+ // 逆地理请求超时时间,最低2s,此处设置为10s
|
|
|
+ _aMapManager.reGeocodeTimeout = locationMode.intValue;
|
|
|
if(locationMode.intValue ==0){
|
|
|
- [_manager setDesiredAccuracy:kCLLocationAccuracyThreeKilometers];
|
|
|
+ [_aMapManager setDesiredAccuracy:kCLLocationAccuracyThreeKilometers];
|
|
|
}else if(locationMode.intValue ==1){
|
|
|
- [_manager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
|
|
|
+ [_aMapManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
|
|
|
}else{
|
|
|
- [_manager setDesiredAccuracy:kCLLocationAccuracyBest];
|
|
|
+ [_aMapManager setDesiredAccuracy:kCLLocationAccuracyBest];
|
|
|
}
|
|
|
if(isOnceLocation.boolValue){
|
|
|
- [_manager requestLocationWithReGeocode:isNeedAddress.boolValue completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
|
|
|
+ NSLog(@"一次定位");
|
|
|
+ [_aMapManager requestLocationWithReGeocode:isNeedAddress.boolValue completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
|
|
|
[self handleResult:location regeocode:regeocode error:error];
|
|
|
|
|
|
}];
|
|
|
}else{
|
|
|
- [_manager startUpdatingLocation];
|
|
|
+ [_aMapManager startUpdatingLocation];
|
|
|
}
|
|
|
result(nil);
|
|
|
|
|
|
}
|
|
|
|
|
|
- (void)handleResult:(CLLocation *)location regeocode:(AMapLocationReGeocode *)regeocode error:(NSError *)error{
|
|
|
+ NSLog(@"定位返回");
|
|
|
if(error){
|
|
|
NSLog(@"error:%@",error);
|
|
|
- [self.channel invokeMethod:@"location" arguments:@""];
|
|
|
+ [_flutterChannel invokeMethod:@"location" arguments:@""];
|
|
|
}else{
|
|
|
NSMutableDictionary* result = [NSMutableDictionary dictionary];
|
|
|
result[@"lon"] =@(location.coordinate.longitude);
|
|
|
@@ -97,7 +116,7 @@
|
|
|
// result[@"accuracy"]=location.accur;
|
|
|
// result[@"isOffset"]=location.;
|
|
|
NSLog(@"reGeocode:%@", regeocode);
|
|
|
- [self.channel invokeMethod:@"location" arguments:[self convertToJsonData:result]];
|
|
|
+ [_flutterChannel invokeMethod:@"location" arguments:[self convertToJsonData:result]];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -118,11 +137,10 @@
|
|
|
|
|
|
- (void)closeLocation:(FlutterMethodCall*)call result:(FlutterResult)result{
|
|
|
NSLog(@"结束定位");
|
|
|
- if(_manager){
|
|
|
- _manager.delegate=nil;
|
|
|
- [_manager stopUpdatingLocation];
|
|
|
- _manager = nil;
|
|
|
-
|
|
|
+ if(_aMapManager){
|
|
|
+ _aMapManager.delegate=nil;
|
|
|
+ [_aMapManager stopUpdatingLocation];
|
|
|
+ _aMapManager = nil;
|
|
|
}
|
|
|
result(nil);
|
|
|
}
|