iOS 地图与定位开发系列教程(二)
下面我们来看一个位置类的基本步骤:
一、启动定位服务
CLLocationManager *locManager = [[CLLocationManager alloc] init]; locManager.delegate = self; [locManager startUpdatingLocation];
二、获得位置信息
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation: (CLLocation *)oldLocation { NSTimeInterval howRecent = [newLocation.timestamp timeIntervalSinceNow]; if(howRecent < -10) return ; //离上次更新的时间少于10秒 if(newLocation.horizontalAccuracy > 100) return; //精度> 100米 //经度和纬度 double lat = newLocation.coordinate.latitude; double lon = newLocation.coordinate.longitude; }
三、获得方向信息(比如往南走)
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { //获得方向 CLLocationDirection heading = newHeading .trueHeading; }
四、停止定位
[locManager stopUpdatingLocation];
你可以设置你想要的精度和距离过滤:
locManager.desiredAccuracy = kLLocationAccuracyBest; locManager.distanceFilter = 1000;