设为首页 加入收藏

TOP

CoreLocation 定位(五)
2017-10-10 12:15:33 】 浏览:8589
Tags:CoreLocation定位
LLocationManager.isMonitoringAvailableForClass(region.classForCoder) { // 开始监听区域 locationMgr.startMonitoringForRegion(region) } } // 进入区域 func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) { print("进入监听区域") } // 离开区域 func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) { print("离开监听区域") } // 区域状态改变 func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion) { if state == .Unknown { print("未识别") } if state == .Inside { print("在区域内") } if state == .Outside { print("在区域外") } }
  • 注意:
    • 必须请求用户定位授权
    • 使用前先判断区域监听是否可用
    • 判断区域半径是否大于最大监听区域,如果大于最大监听区域范围,则无法监听成功

地理编码和反地理编码

  • 地理编码:指根据地质关键字,将其转换成对应的经纬度等信息
  • 反地理编码:指根据经纬度信息,将其转换成对应的省市区等信息
  • CLPlacemark(地表对象)
    • location:CLLocation类型,位置对象的信息,包含经纬度,海拔等
    • region:CLRegion类型,地表对象对应区域
    • addressDictionary:NSDictionary类型,存放省市,街道等信息
    • name:NSString类型,地址全称
    • thoroughfare:NSString类型,街道名称
    • locality:NSString类型,城市名称
    • administrativeArea:NSString类型,省名称
    • country:NSString类型,国家名称
  • 注意
    • 必须联网
    • 有时候反地理编码时会找不到对应信息,需要尝试更换经纬度

OC:

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    // 地理编码
    [geocoder geocodeAddressString:@"福建省厦门市" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        CLPlacemark *placeM = [placemarks lastObject];
        
        NSLog(@"维度:%@ -- 经度:%@", @(placeM.location.coordinate.latitude).stringValue, @(placeM.location.coordinate.longitude).stringValue);
    }];
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    // 反地理编码
    CLLocationDegrees latitude = 24.490474;
    CLLocationDegrees longitude = 118.11022;
    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        NSLog(@"地址:%@", [placemarks firstObject].name);
    }];

swift:

let geocoder = CLGeocoder()
        // 地理编码
        geocoder.geocodeAddressString("福建省厦门市") { (placemarks, error) in
            
            let placeM = placemarks?.last
            
            print("维度\(placeM?.location?.coordinate.latitude) -- 经度\(placeM?.location?.coordinate.longitude)")
        }
        
        // 反地理编码
        let latitude : CLLocationDegrees = 24.490474
        let longitude : CLLocationDegrees = 118.11022
        let location = CLLocation(latitude: latitude, longitude: longitude)
        
        geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
            print("地址:\(placemarks?.first?.name)")
        }
        
    }

先到这,最近太忙,过两天找个时间根据定位做个小项目再分享出来
小项目地址

首页 上一页 2 3 4 5 下一页 尾页 5/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Swift基础(二) 下一篇swift - 导航栏设置

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目