1.ios中自带的地图为高德地图
1>.添加框架CoreLocation.framework框架。
2>.添加头文件:#import
3>.添加代理:<CLLocationManagerDelegate>
4>.在Info.Plist里添加:Privacy - Location Always Usage Description
2.代码:
1>初始化代码:
if(!_locationManager){
self.locationManager = [[CLLocationManageralloc] init];
if([self.locationManagerrespondsToSelector:@selector(requestWhenInUseAuthorization)]){
[self.locationManagerrequestWhenInUseAuthorization];
[self.locationManagerrequestAlwaysAuthorization];
}
//设置代理
[self.locationManagersetDelegate:self];
//设置定位精度
[self.locationManagersetDesiredAccuracy:kCLLocationAccuracyBest];
//设置距离筛选
[self.locationManagersetDistanceFilter:200];
//开始定位
[self.locationManagerstartUpdatingLocation];
//设置开始识别方向
[self.locationManagerstartUpdatingHeading];
}
}
2>.代理实现
#pragma mark - CLLocationManangerDelegate
//定位成功以后调用
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
[self.locationManagerstopUpdatingLocation];
CLLocation* location = locations.lastObject;
[self reverseGeocoder:location];
}
#pragma mark Geocoder
- (void)reverseGeocoder:(CLLocation *)currentLocation {
CLGeocoder* geocoder = [[CLGeocoderalloc] init];
[geocoder reverseGeocodeLocation:currentLocationcompletionHandler:^(NSArray *placemarks,NSError *error) {
if(error || placemarks.count ==0){
NSLog(@"error = %@",error);
}else{
CLPlacemark* placemark = placemarks.firstObject;
NSLog(@"省:%@",[[placemarkaddressDictionary] objectForKey:@"State"]);
NSLog(@"您所在的城市为:%@",[[placemarkaddressDictionary]objectForKey:@"City"]);
}
}];
}
参考demo连接:http://download.csdn.net/detail/kan_crystal/9741582