1. 在 info.plist里加入对应的缺省字段 ,值设置为YES(前台定位写上边字段,前后台定位写下边字段)
NSLocationWhenInUseUsageDescription //允许在前台获取GPS的描述
[locationManager requestWhenInUseAuthorization];// 前台定位
NSLocationWhenInUseUsageDescription //允许在前台获取GPS的描述
NSLocationAlwaysUsageDescription //允许在前、后台获取GPS的描述
2. 调用请求:
//设置定位权限 仅ios8有意义[locationManager requestWhenInUseAuthorization];// 前台定位
[locationManager requestAlwaysAuthorization];// 前后台同时定位
3. 添加坐标点:Product->Scheme->EditScheme
Options->Default Location->(选择一个地点)
4.添加Framework
MapKit.framework
CoreLocation.framework
代码片段
-(IBAction)StartGPS:(id)sender
{
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
// [self.locationManager requestAlwaysAuthorization];//前台和后台都要进行定位
[self.locationManager requestWhenInUseAuthorization];//只有在前台的时候才进行定位
}
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}
- (IBAction)StopGPS:(id)sender
{
[self.locationManager stopUpdatingLocation];
}
#pragma mark - CLLocationDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations objectAtIndex:0];
[self.mapView setCenterCoordinate:location.coordinate animated:YES];
//定义显示的范围
MKCoordinateSpan theSpan;
theSpan.latitudeDelta=0.1;
theSpan.longitudeDelta=0.1;
//定义一个区域(用定义的经纬度和范围来定义)
MKCoordinateRegion theRegion;
theRegion.center=location.coordinate;
theRegion.span=theSpan;
[self.mapView setRegion:theRegion animated:YES];
}