123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- //
- // LocationManage.m
- // MIT_Endorsement
- //
- // Created by 翟玉磊 on 2018/2/8.
- // Copyright © 2018年 翟玉磊. All rights reserved.
- //
- #import "LocationManage.h"
- #import "JZLocationConverter.h" /// 地图坐标转换
- @interface LocationManage ()<AMapLocationManagerDelegate>
- @property (nonatomic, strong) AMapLocationManager *locationManager;
- @property (nonatomic, readwrite, strong) AMapLocationReGeocode *geoCodeSearch;
- @property (nonatomic, readwrite, copy) NSString *currentAreaId;
- @property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate;
- @end
- @implementation LocationManage
- + (instancetype)sharedInstance {
- static LocationManage *locationManage = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- locationManage = [[LocationManage alloc] init];
- });
- return locationManage;
- }
- - (instancetype)init {
- if (self = [super init]) {
- [AMapServices sharedServices].apiKey = [XYChannelInfoManager sharedInstance].location_Key;
- if (_locationManager == nil) {
- _locationManager = [[AMapLocationManager alloc] init];
- _locationManager.delegate = self;
- _locationManager.distanceFilter = kCLDistanceFilterNone;
- _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
- _locationManager.pausesLocationUpdatesAutomatically = NO;
- _locationManager.allowsBackgroundLocationUpdates = NO;
- _locationManager.locationTimeout = 2;
- _locationManager.reGeocodeTimeout = 10;
- }
- }
- return self;
- }
- #pragma mark - Public
- - (void)startContinuousLocation {
- if ([SystemRightObject isLoacationRight]) {
- [self.locationManager setLocatingWithReGeocode:YES];
- [self.locationManager startUpdatingLocation];
- }
- }
- - (void)startSingleLocation{
- if ([SystemRightObject isLoacationRight]) {
- // 定位服务已开启
- [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
- if (error)
- {
- NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
- if (error.code == AMapLocationErrorLocateFailed)
- {
- return;
- }
- }
- // 保存定位信息
- [self saveLocation:location reGeocode:regeocode];
- }];
- }
- }
- - (void)stopLoaction {
- [self.locationManager stopUpdatingLocation];
- }
- - (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
- {
- // 保存定位信息
- [self saveLocation:location reGeocode:reGeocode];
- }
- /// 保存定位信息
- - (void)saveLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode {
- if (location) {
- _coordinate = location.coordinate;
- [[NSUserDefaults standardUserDefaults] setDouble:_coordinate.longitude forKey:@"LOCATION_COORDINATE_LONGITUDE"];
- [[NSUserDefaults standardUserDefaults] setDouble:_coordinate.latitude forKey:@"LOCATION_COORDINATE_LATITUDE"];
- dispatch_async(dispatch_get_main_queue(), ^{
- if (self.didLocationCompletionBlock) {
- self.didLocationCompletionBlock(reGeocode);
- }
- });
- NSLog(@"location:%@", location);
- }
- if (reGeocode)
- {
- [XYLiveManger shareInstance].currentCity = reGeocode.city;
- self.locationFullAddress = reGeocode.formattedAddress;
- NSString *tempText = [reGeocode.adcode substringToIndex:reGeocode.adcode.length - 2];
- NSString *areaId = [NSString stringWithFormat:@"%@", tempText];
- _currentAreaId = [areaId copy];
- NSLog(@"reGeocode:%@", reGeocode);
- }
- }
- #pragma mark - Get
- - (NSString *)currentAreaId {
- if (StringIsEmpty(_currentAreaId)) {
- NSString *areadId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LOCATION_COORDINATE_AREAID"];
- _currentAreaId = [areadId copy];
- return _currentAreaId;
- }
- return _currentAreaId;
- }
- - (CLLocationCoordinate2D)coordinate {
- if (_coordinate.latitude == 0 || _coordinate.longitude == 0) {
- double latitude = [[[NSUserDefaults standardUserDefaults] objectForKey:@"LOCATION_COORDINATE_LATITUDE"] doubleValue];
- double longitude = [[[NSUserDefaults standardUserDefaults] objectForKey:@"LOCATION_COORDINATE_LONGITUDE"] doubleValue];
- _coordinate = CLLocationCoordinate2DMake(latitude, longitude);
- return _coordinate;
- }
- return _coordinate;
- }
- #pragma mark - 导航
- + (void)navigationActionWithCoordinate:(CLLocationCoordinate2D)coordinate loactionName:(NSString *)loactionName presentController:(UIViewController *)presentController {
- NSString *urlScheme = @"xikeApp://";
- NSString *appName = APP_NAME;
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择地图" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
- if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]])
- {
- UIAlertAction *action = [UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- CLLocationCoordinate2D gps = [JZLocationConverter bd09ToWgs84:CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude)];
- MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
- MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:gps addressDictionary:nil]];
- toLocation.name = loactionName;
- [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
- launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
- }];
- [alert addAction:action];
- }
- if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
- {
- UIAlertAction *action = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- 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];
- NSLog(@"%@",urlString);
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
- }];
- [alert addAction:action];
- }
- if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])
- {
- UIAlertAction *action = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSLog(@"%@",urlString);
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
- }];
- [alert addAction:action];
- }
- if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]])
- {
- UIAlertAction *action = [UIAlertAction actionWithTitle:@"谷歌地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSLog(@"%@",urlString);
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
- }];
- [alert addAction:action];
- }
- UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
- [alert addAction:action];
- [presentController presentViewController:alert animated:YES completion:^{
- }];
- }
- + (void)navAppleMapWithCoordinate:(CLLocationCoordinate2D)coordinate loactionName:(NSString *)loactionName
- {
- CLLocationCoordinate2D gps = [JZLocationConverter bd09ToWgs84:CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude)];
- MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
- MKMapItem *toLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:gps addressDictionary:nil] ];
- toLocation.name = loactionName;
- NSArray *items = @[currentLoc,toLocation];
- NSDictionary *dic = @{
- MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
- MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
- MKLaunchOptionsShowsTrafficKey : @(YES)
- };
- [MKMapItem openMapsWithItems:items launchOptions:dic];
- }
- @end
|