LocationManage.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // LocationManage.m
  3. // MIT_Endorsement
  4. //
  5. // Created by 翟玉磊 on 2018/2/8.
  6. // Copyright © 2018年 翟玉磊. All rights reserved.
  7. //
  8. #import "LocationManage.h"
  9. #import "JZLocationConverter.h" /// 地图坐标转换
  10. @interface LocationManage ()<AMapLocationManagerDelegate>
  11. @property (nonatomic, strong) AMapLocationManager *locationManager;
  12. @property (nonatomic, readwrite, strong) AMapLocationReGeocode *geoCodeSearch;
  13. @property (nonatomic, readwrite, copy) NSString *currentAreaId;
  14. @property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate;
  15. @end
  16. @implementation LocationManage
  17. + (instancetype)sharedInstance {
  18. static LocationManage *locationManage = nil;
  19. static dispatch_once_t onceToken;
  20. dispatch_once(&onceToken, ^{
  21. locationManage = [[LocationManage alloc] init];
  22. });
  23. return locationManage;
  24. }
  25. - (instancetype)init {
  26. if (self = [super init]) {
  27. [AMapServices sharedServices].apiKey = [XYChannelInfoManager sharedInstance].location_Key;
  28. if (_locationManager == nil) {
  29. _locationManager = [[AMapLocationManager alloc] init];
  30. _locationManager.delegate = self;
  31. _locationManager.distanceFilter = kCLDistanceFilterNone;
  32. _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  33. _locationManager.pausesLocationUpdatesAutomatically = NO;
  34. _locationManager.allowsBackgroundLocationUpdates = NO;
  35. _locationManager.locationTimeout = 2;
  36. _locationManager.reGeocodeTimeout = 10;
  37. }
  38. }
  39. return self;
  40. }
  41. #pragma mark - Public
  42. - (void)startContinuousLocation {
  43. if ([SystemRightObject isLoacationRight]) {
  44. [self.locationManager setLocatingWithReGeocode:YES];
  45. [self.locationManager startUpdatingLocation];
  46. }
  47. }
  48. - (void)startSingleLocation{
  49. if ([SystemRightObject isLoacationRight]) {
  50. // 定位服务已开启
  51. [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
  52. if (error)
  53. {
  54. NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
  55. if (error.code == AMapLocationErrorLocateFailed)
  56. {
  57. return;
  58. }
  59. }
  60. // 保存定位信息
  61. [self saveLocation:location reGeocode:regeocode];
  62. }];
  63. }
  64. }
  65. - (void)stopLoaction {
  66. [self.locationManager stopUpdatingLocation];
  67. }
  68. - (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
  69. {
  70. // 保存定位信息
  71. [self saveLocation:location reGeocode:reGeocode];
  72. }
  73. /// 保存定位信息
  74. - (void)saveLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode {
  75. if (location) {
  76. _coordinate = location.coordinate;
  77. [[NSUserDefaults standardUserDefaults] setDouble:_coordinate.longitude forKey:@"LOCATION_COORDINATE_LONGITUDE"];
  78. [[NSUserDefaults standardUserDefaults] setDouble:_coordinate.latitude forKey:@"LOCATION_COORDINATE_LATITUDE"];
  79. dispatch_async(dispatch_get_main_queue(), ^{
  80. if (self.didLocationCompletionBlock) {
  81. self.didLocationCompletionBlock(reGeocode);
  82. }
  83. });
  84. NSLog(@"location:%@", location);
  85. }
  86. if (reGeocode)
  87. {
  88. [XYLiveManger shareInstance].currentCity = reGeocode.city;
  89. self.locationFullAddress = reGeocode.formattedAddress;
  90. NSString *tempText = [reGeocode.adcode substringToIndex:reGeocode.adcode.length - 2];
  91. NSString *areaId = [NSString stringWithFormat:@"%@", tempText];
  92. _currentAreaId = [areaId copy];
  93. NSLog(@"reGeocode:%@", reGeocode);
  94. }
  95. }
  96. #pragma mark - Get
  97. - (NSString *)currentAreaId {
  98. if (StringIsEmpty(_currentAreaId)) {
  99. NSString *areadId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LOCATION_COORDINATE_AREAID"];
  100. _currentAreaId = [areadId copy];
  101. return _currentAreaId;
  102. }
  103. return _currentAreaId;
  104. }
  105. - (CLLocationCoordinate2D)coordinate {
  106. if (_coordinate.latitude == 0 || _coordinate.longitude == 0) {
  107. double latitude = [[[NSUserDefaults standardUserDefaults] objectForKey:@"LOCATION_COORDINATE_LATITUDE"] doubleValue];
  108. double longitude = [[[NSUserDefaults standardUserDefaults] objectForKey:@"LOCATION_COORDINATE_LONGITUDE"] doubleValue];
  109. _coordinate = CLLocationCoordinate2DMake(latitude, longitude);
  110. return _coordinate;
  111. }
  112. return _coordinate;
  113. }
  114. #pragma mark - 导航
  115. + (void)navigationActionWithCoordinate:(CLLocationCoordinate2D)coordinate loactionName:(NSString *)loactionName presentController:(UIViewController *)presentController {
  116. NSString *urlScheme = @"xikeApp://";
  117. NSString *appName = APP_NAME;
  118. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择地图" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  119. if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]])
  120. {
  121. UIAlertAction *action = [UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  122. CLLocationCoordinate2D gps = [JZLocationConverter bd09ToWgs84:CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude)];
  123. MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
  124. MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:gps addressDictionary:nil]];
  125. toLocation.name = loactionName;
  126. [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
  127. launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
  128. }];
  129. [alert addAction:action];
  130. }
  131. if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
  132. {
  133. UIAlertAction *action = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  134. NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=%@&mode=driving&coord_type=gcj02",coordinate.latitude, coordinate.longitude, loactionName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  135. NSLog(@"%@",urlString);
  136. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
  137. }];
  138. [alert addAction:action];
  139. }
  140. if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])
  141. {
  142. UIAlertAction *action = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  143. NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  144. NSLog(@"%@",urlString);
  145. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
  146. }];
  147. [alert addAction:action];
  148. }
  149. if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]])
  150. {
  151. UIAlertAction *action = [UIAlertAction actionWithTitle:@"谷歌地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  152. NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  153. NSLog(@"%@",urlString);
  154. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
  155. }];
  156. [alert addAction:action];
  157. }
  158. UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  159. [alert addAction:action];
  160. [presentController presentViewController:alert animated:YES completion:^{
  161. }];
  162. }
  163. + (void)navAppleMapWithCoordinate:(CLLocationCoordinate2D)coordinate loactionName:(NSString *)loactionName
  164. {
  165. CLLocationCoordinate2D gps = [JZLocationConverter bd09ToWgs84:CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude)];
  166. MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
  167. MKMapItem *toLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:gps addressDictionary:nil] ];
  168. toLocation.name = loactionName;
  169. NSArray *items = @[currentLoc,toLocation];
  170. NSDictionary *dic = @{
  171. MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
  172. MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
  173. MKLaunchOptionsShowsTrafficKey : @(YES)
  174. };
  175. [MKMapItem openMapsWithItems:items launchOptions:dic];
  176. }
  177. @end