AmapLocationPlugin.m 6.2 KB

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