| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- //
- // LocationHelper.m
- // olclinic
- //
- // Created by hurricaner on 15/9/5.
- // Copyright (c) 2015年 onecloud.ltd. All rights reserved.
- //
- #import "MOLocationHelper.h"
- @implementation MOLocationHelper
- + (MOLocationHelper *)shareLocationHelper
- {
- static MOLocationHelper *instance = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^
- {
- instance = [[MOLocationHelper alloc] init];
- [instance locationManager];
- });
- return instance;
- }
- - (CLLocationManager *)locationManager
- {
- if(_locationManager == nil)
- {
- _locationManager = [[CLLocationManager alloc]init];
- _locationManager.delegate = self;
- [_locationManager requestWhenInUseAuthorization];
- }
- return _locationManager;
- }
- - (void)getLocationAndBlock:(void (^)(double, double))block
- {
- self.getLocation = block;
-
- self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
- self.locationManager.distanceFilter = kCLDistanceFilterNone;
- [self.locationManager startUpdatingLocation];
- }
- + (void)getCityNameFromLocation:(CLLocation *)location AndBlock:(void (^)(NSString *))block {
- CLGeocoder *geocoder = [[CLGeocoder alloc] init];
- [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
- if (error) {
- MOLogV(@"Geocoding error: %@", error.localizedDescription);
- block(@"");
- return;
- }
- if (placemarks.count > 0) {
- CLPlacemark *placemark = placemarks.firstObject;
- NSString *cityName = placemark.locality; // 获取城市名
- MOLogV(@"City Name: %@", cityName);
- block(cityName);
- // 在这里可以使用cityName做你想要的操作
- }
- }];
- }
- #pragma mark - CLLocationManagerDelegate
- // 地理位置发生改变时触发
- - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
- {
- // 停止位置更新
- [self.locationManager stopUpdatingLocation];
- CLLocation * currLocation = [locations lastObject];
- if (self.getLocation)
- {
- self.getLocation(currLocation.coordinate.latitude,currLocation.coordinate.longitude);
- self.getLocation = nil;
- }
- }
- // 定位失误时触发
- - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
- {
- // 停止位置更新
- [self.locationManager stopUpdatingLocation];
- if (self.getLocation)
- {
- self.getLocation(0,0);
- self.getLocation = nil;
- }
- MOLogV(@"error:%@",error);
- }
- #pragma mark - CLLocationManagerDelegate
- - (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager {
- CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
- // 处理授权状态变化
- if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {
- // 执行你的地理位置相关逻辑
- MOLogV(@"Location services are authorized.");
-
- self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
- self.locationManager.distanceFilter = kCLDistanceFilterNone;
- [self.locationManager startUpdatingLocation];
-
- }
- else{
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"定位提示" message:@"请您在“设置”的“隐私”中开启定位服务,为您提供更多的周边服务" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
- [alert show];
- }
- }
- @end
|