当前位置:文档之家› 定位、地理编码、逆地理编码

定位、地理编码、逆地理编码

UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}";
UIKeyboardFrameChangedByUserInteraction = 0;
//键盘弹出
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";//键盘刚出来那一刻的frame
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
*/
- (void)keyBoardChanged:(NSNotification *)sender
{
UIView *view = [self findFirstResponder:self.view];
self.detailLocTextField.text = ;
}
}];
}
/**
*逆地理编码
*
* @param latitude纬度
* @param longitude经度
//如果是iOS8以上系统
if ([[UIDevice currentDevice].systemVersion floatValue] > 8.0) {
//requestAlwaysAuthorization:总是允许访问地理信息,需要在plist文件中设置NSLocationAlwaysUsageDescription
}
}
#pragma mark -自定义方法
/**
*地理编码
*
* @param str地名
*/
- (void)geoCodingLocationStr:(NSString *)str
{
//地理编码
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:str completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
return nil;
}
if (!_locationManager) {
//创建定位管理者
_locationManager = [[CLLocationManager alloc] init];
//设置管理者代理
_locationManager.delegate = self;
//desiredAccuracy:所需的位置精度。位置服务将尽力实现
[self.locTextField becomeFirstResponder];
//distanceFromLocation:计算两个CLLocation对象的距离
NSLog(@"衡阳到长沙:%f米",[[[CLLocation alloc] initWithLatitude:28.194 longitude:112.982] distanceFromLocation:[[CLLocation alloc] initWithLatitude:29.6 longitude:112.6]]);
CGRect keyBF = [erInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat distance = CGRectGetMaxY(view.frame) - keyBF.origin.y;
//首尾式动画
[UIView beginAnimations:nil context:nil];
//监听键盘通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardChanged:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
/*
UIKeyboardAnimationCurveUserInfoKey = 7; //动画的执行节奏(速度)
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//distanceFilter:指定米的最小更新距离。distanceFilter的值越小,应用程序的耗电量就越大。
_locationManager.distanceFilter = 1000.0f;
titudeTextField.text = [NSString stringWithFormat:@"%f",titude];
self.longitudeTextField.text = [NSString stringWithFormat:@"%f",placeMark.location.coordinate.longitude];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.locationManager startUpdatingLocation];
}
#pragma mark -触摸点击方法
- (IBAction)locationAction:(UIButton *)sender {
if (ror) {
[self showAlertViewControllerWithMessage:@"您的地址输入有误!"];
}
else
{ห้องสมุดไป่ตู้
//注意取出最前面的地址(准确)
//CLPlacemark:代表一个地理位置的地标数据。
CLPlacemark *placeMark = [placemarks firstObject];
kCLAuthorizationStatusDenied:用户已经明确地拒绝了该应用程序的授权,或者
在设置中禁用位置服务。
kCLAuthorizationStatusNotDetermined:用户尚未对该应用程序作出选择
*/
NSLog(@"%i,%i",[CLLocationManager locationServicesEnabled],[CLLocationManager authorizationStatus]);
}
}
- (IBAction)reGeocodingAction:(UIButton *)sender {
[self.view endEditing:YES];
if ((![self isBlinkStr:self.reLatitudeTextField.text] && (![self isBlinkStr:self.reLongitudeTextField.text]))) {
*/
[self.locationManager startUpdatingLocation];
}
- (IBAction)geocodingAction:(UIButton *)sender {
if (![self isBlinkStr:self.locTextField.text]) {
[self geoCodingLocationStr:self.locTextField.text];
}
}
return _locationManager;
}
#pragma mark -控制器周期
- (void)dealloc
{
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
/**
*如果是iOS9的系统,定位的代理方法还是不会执行的,因为iOS9引入了新特性App Transport Security (ATS)。
解决方案:首先在Info.plist中添加NSAppTransportSecurity类型Dictionary。
然后在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES
定位、地理编码、逆地理编码
再看代码
- (CLLocationManager *)locationManager
{
//是否允许定位
/**
* [CLLocationManager locationServicesEnabled]确定用户是否已启用位置服务。
* [CLLocationManager authorizationStatus]返回调用应用程序的当前授权状态。
// [_locationManager requestAlwaysAuthorization];
//requestWhenInUseAuthorization:在使用应用期间允许访问地理信息,需要在plist文件中设置NSLocationWhenInUseUsageDescription
[_locationManager requestWhenInUseAuthorization];
相关主题