美文网首页
简单获取定位-iOS

简单获取定位-iOS

作者: HAKA | 来源:发表于2017-01-06 11:42 被阅读27次
添加两个静态库.png
导入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"]);

      }

  }];

}

相关文章

网友评论

      本文标题:简单获取定位-iOS

      本文链接:https://www.haomeiwen.com/subject/quitbttx.html