2013/4/22记录:
注意:如非必要,不应该多次轮询位置信息i
使用时,可根据需要制定精度,通过仔细制定所需的绝对最低精度级别,可以纺织不必要的电池消耗。
导入:CoreLocation.framework
导入头文件:#import <CoreLocation/CoreLocation.h>
实现CLLocationManagerDelegate协议
self.locationManager= [[CLLocationManager alloc] init]; //位置管理器实例
locationManager.delegate = self; //设置委托
locationManager.desiredAccuracy = kCLLocationAccuracyBest; //设置请求制定精度级别,精度越高越耗电
[locationManager startUpdatingLocation]; //启动
回调方法:
#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
//当这个实施时下面那个就失效。
//- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//
//}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation { if (startingPoint == nil) //如果没有开始坐标,选择刚定位到的坐标作为开始坐标
self.startingPoint = newLocation; //纬度值
NSString *latitudeString = [NSStringstringWithFormat:@"%g\u00B0",
newLocation.coordinate.latitude]; //“\u00B0”:角度符的Unicode表示形式
latitudeLabel.text = latitudeString; //经度值
NSString *longitudeString = [NSStringstringWithFormat:@"%g\u00B0",
newLocation.coordinate.longitude];
longitudeLabel.text = longitudeString; //以coordinate为中心的圆的半径
NSString *horizontalAccuracyString = [NSStringstringWithFormat:@"%gm",
newLocation.horizontalAccuracy];
horizontalAccuracyLabel.text = horizontalAccuracyString; //海拔
NSString *altitudeString = [NSStringstringWithFormat:@"%gm",
newLocation.altitude];
altitudeLabel.text = altitudeString; //海拔方面的精度
NSString *verticalAccuracyString = [NSStringstringWithFormat:@"%gm",
newLocation.verticalAccuracy];
verticalAccuracyLabel.text = verticalAccuracyString; //更新位置的时间戳
NSLog(@"%lf\n",newLocation.timestamp.timeIntervalSince1970); //两位置距离
CLLocationDistance distance = [newLocation
distanceFromLocation:startingPoint];
NSString *distanceString = [NSStringstringWithFormat:@"%gm", distance];
distanceTraveledLabel.text = distanceString; //停止
[self.locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSString *errorType = (error.code == kCLErrorDenied) ?
@"Access Denied" : @"Unknown Error";
UIAlertView *alert = [[UIAlertViewalloc]
initWithTitle:@"Error getting Location"
message:errorType
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alert show];
}