1.首先
链接CoreLocation.framework。点击工程,在主界面中点击Build Phases,点开 Link Binary With Libraries,点击添加按钮“+”,加入CoreLocation.framework。
2.定位管理初始化
locmanager = [[CLLocationManageralloc]init];
[locmanager setDelegate:self];
[locmanagersetDesiredAccuracy:kCLLocationAccuracyBest];
[locmanager startUpdatingLocation];
3.定位信息获取
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (wasFound)return;
wasFound = YES;
CLLocationCoordinate2D loc = [newLocation coordinate];
latitude.text= [NSString stringWithFormat:@"%f", loc.latitude];//获取纬度
longitude.text= [NSString stringWithFormat:@"%f", loc.longitude];//获取经度
altitude.text = [NSStringstringWithFormat: @"%f", newLocation.altitude];//获取高度
NSString *mapUrl = [NSStringstringWithFormat: @"http://maps.google.com/maps?q=%f,%f", loc.latitude, loc.longitude];
NSURL *url = [NSURLURLWithString:mapUrl];
[[UIApplication sharedApplication] openURL:url];
}