定位、地理编码、逆地理编码
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// [_locationManager requestAlwaysAuthorization];
//requestWhenInUseAuthorization:在使用应用期间允许访问地理信息,需要在plist文件中设置NSLocationWhenInUseUsageDescription
[_locationManager requestWhenInUseAuthorization];
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}"; //键盘显示完毕后的frame
//键盘隐藏
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";
if (erwww.sm136.comror) {
[self showAlertViewControllerWithMessage:@"您的地址输入有误!"];
}
else
{
//注意取出最前面的地址(准确)
//CLPlacemark:代表一个地理位置的地标数据。
CLPlacemark *placeMark = [placemarks firstObject];
UIKeyboardAnimationDurationUserInfoKey = "0.25"; //键盘弹出\隐藏动画所需要的时间
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}";
}
}
#pragma mark -自定义方法
/**
*地理编码
*
* @param str地名
*/
- (void)geoCodingLocationStr:(NSString *)str
{
//地理编码
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:str completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
self.detailLocTextField.text = placeMark.name;
}
}];
}
/**
*逆地理编码
*
* @param latitude纬度
* @param longitude经度
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//distanceFilter:指定米的最小更新距离。distanceFilter的值越小,应用程序的耗电量就越大。
_locationManager.distanceFilter = 1000.0f;
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
*/
- (void)keyBoardChanged:(NSNotification *)sender
{
UIView *view = [self findFirstResponder:self.view];
定位、地理编码、逆地理编码
再看Leabharlann Baidu码
- (CLLocationManager *)locationManager
{
//是否允许定位
/**
* [CLLocationManager locationServicesEnabled]确定用户是否已启用位置服务。
* [CLLocationManager authorizationStatus]返回调用应用程序的当前授权状态。
CGRect keyBF = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat distance = CGRectGetMaxY(view.frame) - keyBF.origin.y;
//首尾式动画
[UIView beginAnimations:nil context:nil];
return nil;
}
if (!_locationManager) {
//创建定位管理者
_locationManager = [[CLLocationManager alloc] init];
//设置管理者代理
_locationManager.delegate = self;
//desiredAccuracy:所需的位置精度。位置服务将尽力实现
self.latitudeTextField.text = [NSString stringWithFormat:@"%f",placeMark.location.coordinate.latitude];
self.longitudeTextField.text = [NSString stringWithFormat:@"%f",placeMark.location.coordinate.longitude];
/**
*如果是iOS9的系统,定位的代理方法还是不会执行的,因为iOS9引入了新特性App Transport Security (ATS)。
解决方案:首先在Info.plist中添加NSAppTransportSecurity类型Dictionary。
然后在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES
if ((![CLLocationManager locationServicesEnabled]) || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
[self showAlertViewControllerWithMessage:@"请到\"设置—隐私—定位服务\"开启本软件的定位服务"];
//如果是iOS8以上系统
if ([[UIDevice currentDevice].systemVersion floatValue] > 8.0) {
//requestAlwaysAuthorization:总是允许访问地理信息,需要在plist文件中设置NSLocationAlwaysUsageDescription
}
}
- (IBAction)reGeocodingAction:(UIButton *)sender {
[self.view endEditing:YES];
if ((![self isBlinkStr:self.reLatitudeTextField.text] && (![self isBlinkStr:self.reLongitudeTextField.text]))) {
}
}
return _locationManager;
}
#pragma mark -控制器周期
- (void)dealloc
{
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
//设置动画
if (distance > 0) {
CGFloat spacing = 8;
self.view.transform = CGAffineTransformMakeTranslation(0, -distance - spacing);
}
else
{
self.view.transform = CGAffineTransformIdentity;
*/
[self.locationManager startUpdatingLocation];
}
- (IBAction)geocodingAction:(UIButton *)sender {
if (![self isBlinkStr:self.locTextField.text]) {
[self geoCodingLocationStr:self.locTextField.text];
kCLAuthorizationStatusDenied:用户已经明确地拒绝了该应用程序的授权,或者
在设置中禁用位置服务。
kCLAuthorizationStatusNotDetermined:用户尚未对该应用程序作出选择
*/
NSLog(@"%i,%i",[CLLocationManager locationServicesEnabled],[CLLocationManager authorizationStatus]);
[self reGeoCodingLatitude:[self.reLatitudeTextField.text doubleValue] andLongitude:[self.reLongitudeTextField.text doubleValue]];
}
else
{
[self showAlertViewControllerWithMessage:@"请输入经纬度!"];
//监听键盘通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardChanged:) name:UIKeyboardWillChangeFrameNotification object:nil];
[self.locTextField becomeFirstResponder];
//distanceFromLocation:计算两个CLLocation对象的距离
NSLog(@"衡阳到长沙:%f米",[[[CLLocation alloc] initWithLatitude:28.194 longitude:112.982] distanceFromLocation:[[CLLocation alloc] initWithLatitude:29.6 longitude:112.6]]);
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}";
UIKeyboardFrameChangedByUserInteraction = 0;
//键盘弹出
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";//键盘刚出来那一刻的frame
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.locationManager startUpdatingLocation];
}
#pragma mark -触摸点击方法
- (IBAction)locationAction:(UIButton *)sender {
}
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
/*
UIKeyboardAnimationCurveUserInfoKey = 7; //动画的执行节奏(速度)
//设置动画时间
[UIView setAnimationDuration:[sender.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
//设置动画节奏
[UIView setAnimationCurve:[sender.userInfo[UIKeyboardAnimationDurationUserInfoKey] integerValue]];
//requestWhenInUseAuthorization:在使用应用期间允许访问地理信息,需要在plist文件中设置NSLocationWhenInUseUsageDescription
[_locationManager requestWhenInUseAuthorization];
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}"; //键盘显示完毕后的frame
//键盘隐藏
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";
if (erwww.sm136.comror) {
[self showAlertViewControllerWithMessage:@"您的地址输入有误!"];
}
else
{
//注意取出最前面的地址(准确)
//CLPlacemark:代表一个地理位置的地标数据。
CLPlacemark *placeMark = [placemarks firstObject];
UIKeyboardAnimationDurationUserInfoKey = "0.25"; //键盘弹出\隐藏动画所需要的时间
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}";
}
}
#pragma mark -自定义方法
/**
*地理编码
*
* @param str地名
*/
- (void)geoCodingLocationStr:(NSString *)str
{
//地理编码
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:str completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
self.detailLocTextField.text = placeMark.name;
}
}];
}
/**
*逆地理编码
*
* @param latitude纬度
* @param longitude经度
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//distanceFilter:指定米的最小更新距离。distanceFilter的值越小,应用程序的耗电量就越大。
_locationManager.distanceFilter = 1000.0f;
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
*/
- (void)keyBoardChanged:(NSNotification *)sender
{
UIView *view = [self findFirstResponder:self.view];
定位、地理编码、逆地理编码
再看Leabharlann Baidu码
- (CLLocationManager *)locationManager
{
//是否允许定位
/**
* [CLLocationManager locationServicesEnabled]确定用户是否已启用位置服务。
* [CLLocationManager authorizationStatus]返回调用应用程序的当前授权状态。
CGRect keyBF = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat distance = CGRectGetMaxY(view.frame) - keyBF.origin.y;
//首尾式动画
[UIView beginAnimations:nil context:nil];
return nil;
}
if (!_locationManager) {
//创建定位管理者
_locationManager = [[CLLocationManager alloc] init];
//设置管理者代理
_locationManager.delegate = self;
//desiredAccuracy:所需的位置精度。位置服务将尽力实现
self.latitudeTextField.text = [NSString stringWithFormat:@"%f",placeMark.location.coordinate.latitude];
self.longitudeTextField.text = [NSString stringWithFormat:@"%f",placeMark.location.coordinate.longitude];
/**
*如果是iOS9的系统,定位的代理方法还是不会执行的,因为iOS9引入了新特性App Transport Security (ATS)。
解决方案:首先在Info.plist中添加NSAppTransportSecurity类型Dictionary。
然后在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES
if ((![CLLocationManager locationServicesEnabled]) || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
[self showAlertViewControllerWithMessage:@"请到\"设置—隐私—定位服务\"开启本软件的定位服务"];
//如果是iOS8以上系统
if ([[UIDevice currentDevice].systemVersion floatValue] > 8.0) {
//requestAlwaysAuthorization:总是允许访问地理信息,需要在plist文件中设置NSLocationAlwaysUsageDescription
}
}
- (IBAction)reGeocodingAction:(UIButton *)sender {
[self.view endEditing:YES];
if ((![self isBlinkStr:self.reLatitudeTextField.text] && (![self isBlinkStr:self.reLongitudeTextField.text]))) {
}
}
return _locationManager;
}
#pragma mark -控制器周期
- (void)dealloc
{
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
//设置动画
if (distance > 0) {
CGFloat spacing = 8;
self.view.transform = CGAffineTransformMakeTranslation(0, -distance - spacing);
}
else
{
self.view.transform = CGAffineTransformIdentity;
*/
[self.locationManager startUpdatingLocation];
}
- (IBAction)geocodingAction:(UIButton *)sender {
if (![self isBlinkStr:self.locTextField.text]) {
[self geoCodingLocationStr:self.locTextField.text];
kCLAuthorizationStatusDenied:用户已经明确地拒绝了该应用程序的授权,或者
在设置中禁用位置服务。
kCLAuthorizationStatusNotDetermined:用户尚未对该应用程序作出选择
*/
NSLog(@"%i,%i",[CLLocationManager locationServicesEnabled],[CLLocationManager authorizationStatus]);
[self reGeoCodingLatitude:[self.reLatitudeTextField.text doubleValue] andLongitude:[self.reLongitudeTextField.text doubleValue]];
}
else
{
[self showAlertViewControllerWithMessage:@"请输入经纬度!"];
//监听键盘通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardChanged:) name:UIKeyboardWillChangeFrameNotification object:nil];
[self.locTextField becomeFirstResponder];
//distanceFromLocation:计算两个CLLocation对象的距离
NSLog(@"衡阳到长沙:%f米",[[[CLLocation alloc] initWithLatitude:28.194 longitude:112.982] distanceFromLocation:[[CLLocation alloc] initWithLatitude:29.6 longitude:112.6]]);
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}";
UIKeyboardFrameChangedByUserInteraction = 0;
//键盘弹出
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";//键盘刚出来那一刻的frame
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.locationManager startUpdatingLocation];
}
#pragma mark -触摸点击方法
- (IBAction)locationAction:(UIButton *)sender {
}
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
/*
UIKeyboardAnimationCurveUserInfoKey = 7; //动画的执行节奏(速度)
//设置动画时间
[UIView setAnimationDuration:[sender.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
//设置动画节奏
[UIView setAnimationCurve:[sender.userInfo[UIKeyboardAnimationDurationUserInfoKey] integerValue]];