iOS 获取本地的坐标

iOS 功能真的是非常强大,不需要集成任何第三方的平台与库,就能获取本地的地址坐标值,ios 系统自带的api,就能获取本地的坐标值,

第一步:1、在plist添加  [两个选一个就行,都添加也没事]  value值 为 yes or no
NSLocationAlwaysUsageDescription = 将根据您的地理位置信息,提供精准服务
NSLocationWhenInUseUsageDescription = 若不允许,您将无法使用地图定位等相关的功能

第二步:首先在在.m文件当中 生成实例

导入  #import <CoreLocation/CoreLocation.h>

或者导入   #import <MapKit/MapKit.h>

@interface StoreViewController ()

{

    CLLocationManager *_locationManager;//进行定位管理

    CLLocationCoordinate2D _coordinateCurrent;//上传坐标系统

}

- (void)viewDidLoad {

    [super viewDidLoad];

    [self initGPS];

}

#pragma mark--定位管理

-(void)initGPS {

    //定位管理器

    _locationManager=[[CLLocationManager alloc]init];

    _locationManager.delegate=self;

    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    _locationManager.distanceFilter = 10.0f;

    if ([CLLocationManager locationServicesEnabled]) {

        [_locationManager startUpdatingLocation];

        

        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {

            [_locationManager requestWhenInUseAuthorization];

            _locationManager.desiredAccuracy=kCLLocationAccuracyBest;

            _locationManager.distanceFilter=10.0;

        }

    }else{

        

        NSLog(@"请开启定位功能!");

        

    }


}

#pragma mark --定位代理

//跟踪定位代理方法,每次位置发生变化即会执行(只要定位到相应位置)

//可以通过模拟器设置一个虚拟位置,否则在模拟器中无法调用此方法

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    CLLocation *location=[locations firstObject];//取出第一个位置

    CLLocationCoordinate2D coordinate=location.coordinate;//位置坐标

    NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);

//    如果不需要实时定位,使用完即使关闭定位服务

    CLLocation *loc=[locations lastObject];

    _coordinateCurrent=loc.coordinate;

    

    NSLog(@"====经纬度====%f  %f",_coordinateCurrent.latitude_coordinateCurrent.longitude);

    NSString * str = [NSString stringWithFormat:@"%f",_coordinateCurrent.latitude+_coordinateCurrent.longitude];

    if ([str intValue]==0) {

        [NetManager alertHide:@"亲,你没有开启定位,不能查询附近数据哦"];

        

    }

    else

    {

        [_locationManager stopUpdatingLocation];

        

    }

    

    [self requestDataFromServeWithPage:1 withTye:@"0"];


}

//定位失误时候 触发的方法

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

    

    NSLog(@"error:%@",error);

    if (error) {

        if (_nearDataArray.count==0) {

            [self requestDataFromServeWithPage:1 withTye:@"0"];


        }

     }

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值