iOS 地图的相关知识《二》


地理编码


在处理相关地图的时候,有可能遇到一些问题,比如初次时候的时候,没有弹出地图的提示框,或者没有网络,或者没有显示地图

下面是相关的处理:

/*相关的博客http://my.oschina.net/are1OfBlog/blog/420034#OSC_h1_1

1. 要实现地图、导航功能,往往需要先熟悉定位功能,在iOS中通过Core Location框架进行定位操作。

2. Core Location自身可以单独使用,和地图开发框架MapKit完全是独立的,但是往往地图开发要配合定位框架使用。

3. Core Location中主要包含了定位、地理编码(包括反编码)功能。

 

iOS8中配置配置项发生了变化,可以通过配置

        NSLocationAlwaysUsageDescription

        或者 NSLocationWhenInUseUsageDescription

 来告诉用户使用定位服务的目的,并且注意这个配置是必须的,如果不进行配置则默认情况下应用无法使用定位服务,打开应用不会给出打开定位服务的提示,除非安装后自己设置此应用的定位服务。

 同时,在应用程序中需要根据配置对requestAlwaysAuthorizationlocationServicesEnabled方法进行请求。由于本人机器已经更新到最新的iOS8.1下面的内容主要针对iOS8,使用iOS7的朋友需要稍作调整。

 

还有网络的相关设置

 */



/**

 设置地图的时候 一定要在plist文件里面去设置属性:NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 

 以及网络属性的设置

 */

1.首先导入头文件

#import <CoreLocation/CoreLocation.h>//定位

2.声明熟悉

CLGeocoder *_geocoder;//地理编码


3.设置代理

<CLLocationManagerDelegate>

//地理编码

- (void)geocoderOfMap

{

    _geocoder=[[CLGeocoder alloc]init];

    [self getCoordinateByAddress:@"北京"];

    [self getAddressByLatitude:+39.90498900 longitude:+116.40528500];


}

/*

1 startUpdatingHeading   开始导航方向追踪

 

2 stopUpdatingHeading    停止导航方向追踪

 

3 startMonitoringForRegion 开始对某个区域进行定位追踪,开始对某个区域进行定位后。如果用户进入或者走出某个区域会调用- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

 

 - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region代理方法反馈相关信息

 

4 stopMonitoringForRegion    停止对某个区域进行定位追踪

 

5 requestWhenInUseAuthorization  请求获得应用使用时的定位服务授权,注意使用此方法前在要在info.plist中配置NSLocationWhenInUseUsageDescription

 

6requestAlwaysAuthorization 请求获得应用一直使用定位服务授权,注意使用此方法前要在info.plist中配置NSLocationAlwaysUsageDescription

 */


#pragma mark 根据地名确定地理坐标

-(void)getCoordinateByAddress:(NSString *)address{

    //地理编码

    [_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {

        //取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址

        CLPlacemark *placemark=[placemarks firstObject];

        

        CLLocation *location=placemark.location;//位置

        CLRegion *region=placemark.region;//区域

        NSDictionary *addressDic= placemark.addressDictionary;//详细地址信息字典,包含以下部分信息

        //        NSString *name=placemark.name;//地名

        //        NSString *thoroughfare=placemark.thoroughfare;//街道

        //        NSString *subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等

        //        NSString *locality=placemark.locality; // 城市

        //        NSString *subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑

        //        NSString *administrativeArea=placemark.administrativeArea; //

        //        NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息

        //        NSString *postalCode=placemark.postalCode; //邮编

        //        NSString *ISOcountryCode=placemark.ISOcountryCode; //国家编码

        //        NSString *country=placemark.country; //国家

        //        NSString *inlandWater=placemark.inlandWater; //水源、湖泊

        //        NSString *ocean=placemark.ocean; // 海洋

        //        NSArray *areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标

        NSLog(@"位置:%@,区域:%@,详细信息:%@",location,region,addressDic);

        

        //反编码

        [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

            

            CLPlacemark *placemark=[placemarks firstObject];

            NSLog(@"详细信息:%@",placemark.addressDictionary);

        }];


    }];

    


   

}


#pragma mark 根据坐标取得地名

-(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{

    //反地理编码

    CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];

    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

        CLPlacemark *placemark=[placemarks firstObject];

        NSLog(@"详细信息:%@",placemark.addressDictionary);

    }];

    

    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        NSLog(@"反编码");

    }];

}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值