ios 定位获取当前位置信息

啊,倦怠的人生啊~~

什么事情都没做一眨眼就2点半了啊!!赶紧爬起来写博客啊。

诸位看官会鄙视我么,表示我真心不是把这当技术文章写的啊。

啊,下午我们来第二篇。获取地理位置信息。嗯嗯,秘籍上说叫逆向地理编码,其实就是由坐标位置获取当前所在地的文字信息啦。

至于什么速率怎么算,距离怎么取,那个看文档吧,筒子们。

获取当前所在地的地理位置信息需要使用一个新的类,MKReverseGeocoder。这个类在MapKit.framework中。我们把框架加进来,并将头文件导入就可以用了。

敲了一会代码,结果发现这个类iOS5.0就不用了。真是的。为了照顾兼容性,我们先研究MKReverseGeocoder,等下再来研究这个新类,恩,名字叫做CLGeocoder,恩,没拼错。在CoreLocation里面。

先说MKReverseGeocoder的用法。(忍不住出来吐个槽,一般看见被划掉的红线,一边敲回车,还真是说不出来的违和啊。)

。。。。。。。。。。。。。。。

表示测试完成了,IOS的网络总是假死,一假死,各种需要使用网络的的功能,就不怎好用了。桑心。

MKReverseGeocoder的初始化,很简单。

MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:currentCoordinate2D];

      geocoder.delegate = self;

[geocoder start];

调用以上代码后呢,会自动调用反向地址编码的API。我们这边使用代理来接收。至于代理方法么,我们要实现两个。

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {

}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{

}

第一个方法是获取反向编码的。第二个是当反向编码失败时,处理错误使用的。

我们主要讨论第一个方法。

placemark(MKPlacemark类的对象)其实是geocoder(MKReverseGeocoder类的对象)的一个属性。从geocoder里面取placemark这个和直接取placemark这个其实区别不大。而我们需要的信息主要就在这个里面了。

// 这个字典存放基础数据

@property (nonatomic, readonly) NSDictionary *addressDictionary;

让我们试试看,能从这个字典里面倒出来些什么东西。

以下是我用这个addressDictionary属性倒出来的字典。我们分析看看。

{

    City = "U897fU5b89U5e02";// 城市名字

    Country = "U4e2dU56fd";// 国家名字

    CountryCode = CN;// 国家编码

    FormattedAddressLines =     (

        "U4e2dU56fd",

        "U9655U897fU7701U897fU5b89U5e02U96c1U5854U533a",

        "U9ad8U65b0U516dU8def34U53f7"

    ); // 这个应该是格式化后的地址了

    State = "U9655U897fU7701"; // 省

    Street = "U9ad8U65b0U516dU8def 34U53f7";// 街道完整名称

    SubLocality = "U96c1U5854U533a";//区名

    SubThoroughfare = "34U53f7";//具体地址

    Thoroughfare = "U9ad8U65b0U516dU8def";//街道名称

}

注意:上面的这个字典是可以直接转化为联系人的字典的,通过这个ABCreateStringWithAddressDictionary属性。

以下是placemark的其他属性。大家可以随意取用。

// address dictionary properties

@property (nonatomic, readonly) NSString *name; // eg. Apple Inc.

@property (nonatomic, readonly) NSString *thoroughfare; // street address, eg. 1 Infinite Loop

@property (nonatomic, readonly) NSString *subThoroughfare; // eg. 1

@property (nonatomic, readonly) NSString *locality; // city, eg. Cupertino

@property (nonatomic, readonly) NSString *subLocality; // neighborhood, common name, eg. Mission District

@property (nonatomic, readonly) NSString *administrativeArea; // state, eg. CA

@property (nonatomic, readonly) NSString *subAdministrativeArea; // county, eg. Santa Clara

@property (nonatomic, readonly) NSString *postalCode; // zip code, eg. 95014

@property (nonatomic, readonly) NSString *ISOcountryCode; // eg. US

@property (nonatomic, readonly) NSString *country; // eg. United States

@property (nonatomic, readonly) NSString *inlandWater; // eg. Lake Tahoe

@property (nonatomic, readonly) NSString *ocean; // eg. Pacific Ocean

@property (nonatomic, readonly) NSArray *areasOfInterest; // eg. Golden Gate Park

注意:我在使用的过程中发现,如果网络假死,则有可能较长时间无法获得逆向的结果。这一点可能需要大家注意。

哎呀呀呀,差点忘了,IOS5下不推荐使用我上面讲的一大堆。我们需要用这个CLGeocoder类。

使用方法也很简单。参照如下步骤:

首先创建一个CLGeocoder对象,然后调用他的- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;方法。按照需要的参数类型传参。有的筒子会问这个CLGeocodeCompletionHandler东西怎么写?这个其实是IOS4之后就被官方大力推荐使用的BLOCK,不会用的同学快去看文档吧。

CLGeocodeCompletionHandler的定义就是这样的。typedef void(^CLGeocodeCompletionHandler)(NSArray *placemarks, NSError *error); 我们只要写好一个block对象传进去就好了。

以下是使用CLGeocoder的参考代码。不用代理了是不是很开心呢?

    CLGeocoder* geocoder = [[CLGeocoder alloc] init];

   

    [geocoder reverseGeocodeLocation:newLocation completionHandler:

     ^(NSArray* placemarks, NSError* error){

         NSLog(@"%@",placemarks);

     }];

表示定位部分的内容就介绍到这里,至于什么正向编码,什么前面提到的速率,距离之类的,大家参看文档就好了。勤看文档的筒子才是好筒子嘛

原文地址:https://www.cnblogs.com/changjiang/p/3414220.html