美文网首页
App 中使用 GPS 定位

App 中使用 GPS 定位

作者: zcaaron | 来源:发表于2016-07-09 16:32 被阅读82次

#import <CoreLocation/CoreLocation.h>

.h

@property (nonatomic,strong) CLLocationManager * manager;

@property (nonatomic,assign) BOOL isLocationed;

.m

-(CLLocationManager *)manager{
    if (!_manager) {
        _manager = [[CLLocationManager alloc]init];
        _manager.delegate = self;
        _manager.desiredAccuracy = kCLLocationAccuracyBest;
    }
    return _manager;
}

#pragma mark
#pragma mark - 判断手机是否开启定位功能 然后开始定位
- (void)findLocationCity{
    
    if ([CLLocationManager locationServicesEnabled]) {
        if ([self.manager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
//            NSLog(@"requestWhenInUseAuthorization");
            [self.manager requestWhenInUseAuthorization];
        }
        [self.manager startUpdatingLocation];
//        NSLog(@"start GPS");
    }else{
        NSLog(@"提醒用户:定位服务未开启,可在设置中进行修改....");
    }
    
}

#pragma mark
#pragma mark - 获取 GPS 经度和纬度
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    if (!_isLocationed) {
        
        CLLocation * location1 = [locations lastObject];
        CLLocationCoordinate2D coordinate = location1.coordinate;
        CLGeocoder * geocoder = [[CLGeocoder alloc]init];
        CLLocation * location = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            if (placemarks.count > 0) {
                CLPlacemark * placemark = [placemarks objectAtIndex:0];
                
                NSString * city = placemark.locality;
                
                if (!city) {
                    city = placemark.administrativeArea;
                }
                    
                    UIAlertController * ac = [UIAlertController alertControllerWithTitle:@"切换当前城市:" message:city preferredStyle:UIAlertControllerStyleAlert];
                    
                    UIAlertAction * aa = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                           [WeatherView_Model weatherWithCurrentCity:city finishBlock:^(WeatherView_Model *weatherModel) {
                            self.weather_View.weatherModel = weatherModel;
                        } errorBlock:^{
                        }];
                    }];
                    [ac addAction:aa];
                    
                    UIAlertAction * aa2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                    [ac addAction:aa2];
                    
                    [self presentViewController:ac animated:YES completion:nil];
        
            }else if (error == nil && placemarks.count == 0){
                NSLog(@"No results were returned.");
            }else if (error != nil){
                NSLog(@"An error occurred = %@", error);
            }
        }];
        
        self.isLocationed = YES;
    }
    [self.manager stopUpdatingLocation];
}

#pragma mark
#pragma mark - 定位失败返回
- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error{
    if (error.code == kCLErrorDenied) {
        NSLog(@"Error : %@",error);
    }
}

仅供学习 参考

相关文章

  • App 中使用 GPS 定位

    #import .h .m 仅供学习 参考

  • 手机定位原理

    手机定位的原理有以下几种: 1、GPS定位:手机中一般都有GPS芯片,很多APP要求授权获取GPS位置权限,通过这...

  • 如果需要持续使用后台定位

    在提交审核的app 介绍记得写上 【温馨提示】本App的主要服务会持续使用GPS定位服务,切换至后时,仍会继续,相...

  • 卡鱼:招商加盟

    GPS定位 卡鱼信用卡管家APP自带GPS定位功能,客户在注册时,可以准确定位经纬度。 区域保护 客户注册时只要定...

  • 你所不知道的Excel“GPS定位”

    说到“GPS定位”可能大家都很熟悉,那Excel中的“GPS定位”又是什么呢?顾名思义Excel的GPS定位...

  • GPS定位中

    经过第一堂课的学习,我们学会了怎么做伯乐——如何发现挖掘自己和小伙伴的能力。一个星期里不断的练习非常充实:成...

  • Autoware 中 GPS 定位问题

    问题描述 Autoware 主要使用 LiDAR 定位,在 ndt_matching 程序中实现。 GPS 只是作...

  • 安信可(Ai-Thinker)A9/A9G GPRS模块内部集成

    功能说明 实现手机可以实时查看定位器(A9/A9G模块)位置。 定位器可以使用GPS定位,如果GPS无法定位或者A...

  • RXJava 应用系列-场景2

    二、获取定位信息 场景描述: 首先使用GPS定位,如果定位失败使用IP定位从服务器获取。 问题分析: 此问题可以分...

  • 2016-08-25 LBS定位服务接口

    LBS(基于位置服务) LBS定义 LBS定位不足: GPS定义 GPS定位优点: LBS基站定位和GPS定位的区...

网友评论

      本文标题:App 中使用 GPS 定位

      本文链接:https://www.haomeiwen.com/subject/qeorjttx.html