导入framework.png 添加定位按钮.png delegate.png
#import"ViewController.h"
//导入
#import
#import
//设置代理
@interfaceViewController()
@property(nonatomic,strong)CLLocationManager*locationManager;
//点击获取位置button
@property(nonatomic,strong)UIButton*locationButton;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
_locationButton= [UIButtonbuttonWithType:UIButtonTypeCustom];
_locationButton.frame=CGRectMake(0,100,self.view.bounds.size.width,self.view.bounds.size.width);
_locationButton.backgroundColor= [UIColorredColor];
[_locationButtonsetTitle:@"定位"forState:UIControlStateNormal];
[_locationButtonsetTitleColor: [UIColorwhiteColor]forState:UIControlStateNormal];
[self.viewaddSubview:_locationButton];
[_locationButtonaddTarget:selfaction:@selector(ButtonClickToLocation)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:_locationButton];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)ButtonClickToLocation
{
self.locationManager= [[CLLocationManageralloc]init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
[self.locationManagerrequestAlwaysAuthorization];
self.locationManager.distanceFilter=10.0f;
[self.locationManagerstartUpdatingLocation];
}
- (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray *)locations
{
CLLocation*newLocation = locations[0];
CLLocationCoordinate2DoCoordinate = newLocation.coordinate;
NSLog(@"=旧经度:%f==旧纬度: %f",oCoordinate.longitude,oCoordinate.latitude);
[self.locationManagerstopUpdatingLocation];
//地理位置解码编码器对象
CLGeocoder*geoCoder = [[CLGeocoderalloc]init];
[geoCoderreverseGeocodeLocation:newLocationcompletionHandler:^(NSArray *_Nullableplacemarks,NSError*_Nullableerror) {
for(CLPlacemark*placeinplacemarks) {
NSDictionary*location = [placeaddressDictionary];
NSLog(@"位置信息======%@",location);
NSLog(@"位置信息====City%@==Country%@==CountryCode%@==FormattedAddressLines%@==Name%@==State%@==Street%@===SubLocality%@=====Thoroughfare%@",location[@"City"],location[@"Country"],location[@"CountryCode"],location[@"FormattedAddressLines"][0],location[@"Name"],location[@"State"],location[@"Street"],location[@"SubLocality"],location[@"Thoroughfare"]);
}
}];
}
网友评论