导入包:CoreLocation.framework Mapkit.frameWork
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
CLLocationManagerDelegate
@property(nonatomic,strong)CLLocationManager* locationManager; //位置信息
#pragma mark -
#pragma mark - CLLocationManager delegate
//开始定位
-(void)startLocation{
[StaticTools showHUDInView:self.view HudType:MBProgressHUDModeIndeterminate withText:@"定位中"];
if (![CLLocationManager locationServicesEnabled]||[CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied) {
[StaticTools showAlertWithTag:Alert_Tag_CloseLocationServices title:nil message:@"定位功能未开启" AlertType:CAlertTypeDefault SuperView:self];
//确定
}else{
NSLog(@"定位正常");
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
//最佳精度,最耗电
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//距离过滤,超出设定的方圆100m,就会重新校正当前用户的位置信息
self.locationManager.distanceFilter = 1000.0f;
//发送startupdatingLocation
[self.locationManager startUpdatingLocation];
}
}
//定位出错,经纬度出错
-(void)locationManager:(CLLocationManager *)managerdidFailWithError:(NSError *)error{
[StaticTools removeHUDFromView:self.view];
[self.locationManager stopUpdatingLocation];
NSLog(@"获取经纬度失败,失败原因是%@",[error description]);
[StaticTools showAlertWithTag:Alert_Tag_LocationFail title:nil message:@"定位失败!" AlertType:CAlertTypeDefault SuperView:self];
}
//定位成功时调用
-(void)locationManager:(CLLocationManager *)managerdidUpdateLocations:(NSArray *)locations
{
[StaticTools removeHUDFromView:self.view];
[self.locationManager stopUpdatingLocation];
CLLocation *curr = [locations lastObject];
CLLocationCoordinate2D coord;
NSLog(@"1--%f=====%f",curr.coordinate.latitude,curr.coordinate.longitude);
coord = curr.coordinate;
NSLog(@"2--%f=====%f",coord.latitude,coord.longitude);
CLLocation *location = [[CLLocation alloc]initWithCoordinate:coord altitude:curr.altitude horizontalAccuracy:curr.horizontalAccuracy verticalAccuracy:curr.verticalAccuracy course:curr.course speed:curr.speed timestamp:curr.timestamp];
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark *placemark in placemarks) {
NSLog(@"3--%f====dandan=%f",location.coordinate.latitude,location.coordinate.longitude);
NSLog(@"placemark==dandan=%@",placemark);
//省:
NSString* str=[placemark.addressDictionary objectForKey:@"State"];
NSString* str2=[placemark.addressDictionary objectForKey:@"City"];
if ([str rangeOfString:@"省"].location!=NSNotFound) {
NSString* state=str;
ShareManager.fixedState=state;
NSLog(@"4------------%@",ShareManager.fixedState);
}else{
NSString* state=[str substringToIndex:[str length]-1];
ShareManager.fixedState=state;
NSLog(@"4------111111------%@",ShareManager.fixedState);
}
//市:
if ([str2 rangeOfString:@"市"].location!=NSNotFound) {
NSString* city=str2;
ShareManager.fixedCity=city;
NSLog(@"5------------%@",ShareManager.fixedCity);
}else{
NSString* city=[str substringToIndex:[str length]-1];
ShareManager.fixedCity=city;
NSLog(@"5------111111------%@",ShareManager.fixedCity);
}
}
}];
}