Browse Source

解决IOS定位问题

i2edu 5 years ago
parent
commit
716ddebd6d
4 changed files with 94 additions and 43 deletions
  1. 35 7
      .idea/workspace.xml
  2. 3 1
      example/lib/main.dart
  3. 45 27
      ios/Classes/AmapLocationPlugin.m
  4. 11 8
      lib/amap_location.dart

+ 35 - 7
.idea/workspace.xml

@@ -1,7 +1,12 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
   <component name="ChangeListManager">
-    <list default="true" id="cc871a7e-676b-4f6d-9e12-c09ccbaf84b8" name="Default Changelist" comment="" />
+    <list default="true" id="cc871a7e-676b-4f6d-9e12-c09ccbaf84b8" name="Default Changelist" comment="更改IOS依赖包问题">
+      <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/example/lib/main.dart" beforeDir="false" afterPath="$PROJECT_DIR$/example/lib/main.dart" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/ios/Classes/AmapLocationPlugin.m" beforeDir="false" afterPath="$PROJECT_DIR$/ios/Classes/AmapLocationPlugin.m" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/lib/amap_location.dart" beforeDir="false" afterPath="$PROJECT_DIR$/lib/amap_location.dart" afterDir="false" />
+    </list>
     <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -20,11 +25,6 @@
       </list>
     </option>
   </component>
-  <component name="FindInProjectRecents">
-    <findStrings>
-      <find>location</find>
-    </findStrings>
-  </component>
   <component name="Git.Settings">
     <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
   </component>
@@ -80,7 +80,14 @@
       <option name="project" value="LOCAL" />
       <updated>1585301086929</updated>
     </task>
-    <option name="localTasksCounter" value="3" />
+    <task id="LOCAL-00003" summary="更改IOS依赖包问题">
+      <created>1585301292763</created>
+      <option name="number" value="00003" />
+      <option name="presentableId" value="LOCAL-00003" />
+      <option name="project" value="LOCAL" />
+      <updated>1585301292763</updated>
+    </task>
+    <option name="localTasksCounter" value="4" />
     <servers />
   </component>
   <component name="Vcs.Log.History.Properties">
@@ -110,4 +117,25 @@
     <MESSAGE value="更改IOS依赖包问题" />
     <option name="LAST_COMMIT_MESSAGE" value="更改IOS依赖包问题" />
   </component>
+  <component name="XDebuggerManager">
+    <breakpoint-manager>
+      <breakpoints>
+        <line-breakpoint enabled="true" type="Dart">
+          <url>file://$PROJECT_DIR$/lib/amap_location.dart</url>
+          <line>52</line>
+          <option name="timeStamp" value="3" />
+        </line-breakpoint>
+        <line-breakpoint enabled="true" type="Dart">
+          <url>file://$PROJECT_DIR$/lib/amap_location.dart</url>
+          <line>44</line>
+          <option name="timeStamp" value="4" />
+        </line-breakpoint>
+        <line-breakpoint enabled="true" type="Dart">
+          <url>file://$PROJECT_DIR$/lib/amap_location.dart</url>
+          <line>36</line>
+          <option name="timeStamp" value="5" />
+        </line-breakpoint>
+      </breakpoints>
+    </breakpoint-manager>
+  </component>
 </project>

+ 3 - 1
example/lib/main.dart

@@ -54,11 +54,13 @@ class _MyAppState extends State<MyApp> {
                   options: LocationOption(
                 isOnceLocation: false,
                 locationInterval: 5000,
+                locationTimeOut: 10,
+                locationMode: LocationMode.Battery_Saving,
               ));
               AmapLocation.instance.locationStream.listen((d) {
                 print(d.city);
                 // stop location when isOnceLocation is true
-                // AmapLocation.instance.disposed();
+                 AmapLocation.instance.disposed();
               });
               await Future.delayed(Duration(seconds: 60));
               AmapLocation.instance.disposed();

+ 45 - 27
ios/Classes/AmapLocationPlugin.m

@@ -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);
 }

+ 11 - 8
lib/amap_location.dart

@@ -15,13 +15,20 @@ class AmapLocation {
   StreamController _streamController;
   Stream<LocationResultEntity> get locationStream => _streamController.stream;
 
+  AmapLocation(){
+    _channel.setMethodCallHandler(platformCallHandler);
+    _streamController = StreamController<LocationResultEntity>.broadcast();
+  }
+
   static Future<String> get platformVersion async {
     final String version = await _channel.invokeMethod('getPlatformVersion');
     return version;
   }
 
   Future<void> startLocation({LocationOption options}) async {
-    _setLocationCallback();
+    if (_streamController == null) {
+      _streamController = StreamController<LocationResultEntity>.broadcast();
+    }
     return await _channel.invokeMethod(
         'startLocation', options?.toMap() ?? LocationOption().toMap());
   }
@@ -34,14 +41,8 @@ class AmapLocation {
     return await _channel.invokeMethod("closeLocation");
   }
 
-  void _setLocationCallback() {
-    if (_streamController == null) {
-      _streamController = StreamController<LocationResultEntity>.broadcast();
-    }
-    _channel.setMethodCallHandler(platformCallHandler);
-  }
-
   Future<void> platformCallHandler(MethodCall call) async {
+    print('method:${call.method} , argument s:${call.arguments}');
     try {
       if (call.method == "location") {
         LocationResultEntity entity =
@@ -51,5 +52,7 @@ class AmapLocation {
     } catch (ex) {
       print('Unexpected error: $ex');
     }
+    return null;
+
   }
 }