定位需要使用苹果官方的类库CoreLocation,通过GPS来确定位置信息 并且需要实现CLLocationManagerDelegate协议
1.首先添加类库CoreLocation
2.需要实现协议CLLocationManagerDelegate
设置全局的CLLocationManager就是gps位置管理器 打开 关闭 获取gps经纬度
CLLocationManager *gpsManager;
3.开始定位
-(void)startLocation{ //0.判断 if (![CLLocationManager locationServicesEnabled]) { NSLog(@"没有定位 或者没有打开定位"); } //1.创建一个定位对象 manager = [[CLLocationManager alloc]init]; //设置GPS的精确度 manager.desiredAccuracy = kCLLocationAccuracyBest; //下面是精确度从高到低(越精确越耗电) //kCLLocationAccuracyBestForNavigation //(最精确) //kCLLocationAccuracyBest; //kCLLocationAccuracyNearestTenMeters; //kCLLocationAccuracyHundredMeters; //kCLLocationAccuracyKilometer; //kCLLocationAccuracyThreeKilometers; //2.设置代理 目的值GPS有数据来调用代理方法 manager.delegate = self; //3.更新位置数据 [manager startUpdatingLocation]; } //4.结束定位 -(void)stopLocation{ [manager stopUpdatingLocation]; manager = nil; } #pragma mark - 每次GPS更新位置信息都会被触发 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ for(CLLocation *loc in locations){ //CLLocation 就是一次经纬度 , 方向 海拔 等信息 //loc.coordinate就是取的经纬度 CLLocationCoordinate2D l = loc.coordinate; CLLocationDegrees lat = l.latitude; CLLocationDegrees lng = l.longitude; //海拔 CLLocationDistance altitude = loc.altitude; NSLog(@"GPS (%f, %f, %f)", lat, lng, altitude); } }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/5250.html