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"];
}
}
}