AmapLocationPlugin.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #import "AmapLocationPlugin.h"
  2. @interface AmapLocationPlugin()<AMapLocationManagerDelegate> {
  3. AMapLocationManager* _aMapManager;
  4. FlutterMethodChannel* _flutterChannel;
  5. }
  6. @end
  7. @implementation AmapLocationPlugin
  8. -(instancetype)initWithBinaryMessager:(NSObject<FlutterBinaryMessenger>*)messenger{
  9. if(self == [super init]){
  10. _flutterChannel = [FlutterMethodChannel
  11. methodChannelWithName:@"amap_location"
  12. binaryMessenger:messenger];
  13. }
  14. return self;
  15. }
  16. + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  17. AmapLocationPlugin* instance = [[AmapLocationPlugin alloc] initWithBinaryMessager:registrar.messenger];
  18. [registrar addMethodCallDelegate:instance channel:[instance channel]];
  19. [registrar addApplicationDelegate:instance];
  20. }
  21. -(FlutterMethodChannel *)channel{
  22. return _flutterChannel;
  23. }
  24. - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  25. if ([@"getPlatformVersion" isEqualToString:call.method]) {
  26. result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  27. } else if([@"startLocation" isEqualToString:call.method]){
  28. [self startLocation:call result:result];
  29. } else if([@"closeLocation" isEqualToString:call.method]){
  30. [self closeLocation:call result:result];
  31. }else {
  32. result(FlutterMethodNotImplemented);
  33. }
  34. }
  35. - (void)startLocation:(FlutterMethodCall*)call result:(FlutterResult)result{
  36. NSNumber* isOnceLocation = call.arguments[@"isOnceLocation"];
  37. NSNumber* isNeedAddress = call.arguments[@"isNeedAddress"];
  38. NSNumber* mockEnable = call.arguments[@"mockEnable"];
  39. // NSNumber* locationInterval = call.arguments[@"locationInterval"];
  40. NSNumber* locationTimeOut = call.arguments[@"locationTimeOut"];
  41. NSNumber* locationMode = call.arguments[@"locationMode"];
  42. NSLog(@"开始定位");
  43. _aMapManager = [[AMapLocationManager alloc]init];
  44. _aMapManager.delegate =self;
  45. _aMapManager.detectRiskOfFakeLocation=mockEnable.boolValue;
  46. [_aMapManager setLocatingWithReGeocode:isNeedAddress.boolValue];
  47. // 定位超时时间,最低2s,此处设置为10s
  48. _aMapManager.locationTimeout = locationTimeOut.intValue;
  49. // 逆地理请求超时时间,最低2s,此处设置为10s
  50. _aMapManager.reGeocodeTimeout = locationMode.intValue;
  51. if(locationMode.intValue ==0){
  52. [_aMapManager setDesiredAccuracy:kCLLocationAccuracyThreeKilometers];
  53. }else if(locationMode.intValue ==1){
  54. [_aMapManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
  55. }else{
  56. [_aMapManager setDesiredAccuracy:kCLLocationAccuracyBest];
  57. }
  58. if(isOnceLocation.boolValue){
  59. NSLog(@"一次定位");
  60. [_aMapManager requestLocationWithReGeocode:isNeedAddress.boolValue completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
  61. [self handleResult:location regeocode:regeocode error:error];
  62. }];
  63. }else{
  64. [_aMapManager startUpdatingLocation];
  65. }
  66. result(nil);
  67. }
  68. - (void)handleResult:(CLLocation *)location regeocode:(AMapLocationReGeocode *)regeocode error:(NSError *)error{
  69. NSLog(@"定位返回");
  70. if(error){
  71. NSLog(@"error:%@",error);
  72. [_flutterChannel invokeMethod:@"location" arguments:@""];
  73. }else{
  74. NSMutableDictionary* result = [NSMutableDictionary dictionary];
  75. result[@"lon"] =@(location.coordinate.longitude);
  76. result[@"lat"] =@(location.coordinate.latitude);
  77. NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
  78. if (regeocode)
  79. {
  80. result[@"citycode"]=regeocode.citycode;
  81. result[@"adcode"]=regeocode.adcode;
  82. result[@"country"]=regeocode.country;
  83. result[@"province"]=regeocode.province;
  84. result[@"city"]=regeocode.city;
  85. result[@"district"]=regeocode.district;
  86. // result[@"road"]=regeocode.r;
  87. result[@"street"]=regeocode.street;
  88. result[@"number"]=regeocode.number;
  89. result[@"poiname"]=regeocode.POIName;
  90. result[@"errorCode"]=error==nil?nil:@(error.code);
  91. result[@"errorInfo"]=nil;
  92. // result[@"locationType"]=location.;
  93. // result[@"locationDetail"]=location.detail;
  94. result[@"aoiname"]=regeocode.AOIName;
  95. result[@"address"]=regeocode.formattedAddress;
  96. // result[@"poiid"]=location.poi;
  97. result[@"floor"]=[[NSString alloc]initWithFormat:@"%ld",(long)location.floor.level];
  98. result[@"description"]=regeocode.description;
  99. result[@"time"]=@(location.timestamp.timeIntervalSinceNow);
  100. result[@"provider"]=regeocode.province;
  101. // result[@"accuracy"]=location.accur;
  102. // result[@"isOffset"]=location.;
  103. NSLog(@"reGeocode:%@", regeocode);
  104. [_flutterChannel invokeMethod:@"location" arguments:[self convertToJsonData:result]];
  105. }
  106. }
  107. }
  108. //格式化输出
  109. - (NSString *)convertToJsonData:(NSDictionary *)userInfo {
  110. NSError *error;
  111. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:nil error:&error];
  112. NSString *jsonString=@"";
  113. if (!jsonData) {
  114. NSLog(@"%@", error);
  115. } else {
  116. jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  117. }
  118. return jsonString;
  119. }
  120. - (void)closeLocation:(FlutterMethodCall*)call result:(FlutterResult)result{
  121. NSLog(@"结束定位");
  122. if(_aMapManager){
  123. _aMapManager.delegate=nil;
  124. [_aMapManager stopUpdatingLocation];
  125. _aMapManager = nil;
  126. }
  127. result(nil);
  128. }
  129. - (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error{
  130. NSLog(@"定位出现错误:%@",error);
  131. }
  132. - (void)amapLocationManager:(AMapLocationManager *)manager doRequireLocationAuth:(CLLocationManager*)locationManager{
  133. NSLog(@"申请权限");
  134. [locationManager requestAlwaysAuthorization];
  135. }
  136. - (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode{
  137. [self handleResult:location regeocode:reGeocode error:nil];
  138. }
  139. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
  140. NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
  141. NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:bundlePath];
  142. NSString *gaodeAppKey = [infoDict objectForKey:@"GaoDeAppKey"];
  143. [AMapServices sharedServices].apiKey = gaodeAppKey;
  144. NSLog(@"配置高德地图定位");
  145. return YES;
  146. }
  147. @end