AmapLocationPlugin.m 8.0 KB

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