IOS获取当前地理位置文本

以下内容摘抄自网络,著作权属于原作者

方法1:使用ios自带联网查询功能,断网会报 PBRequester failed with Error Error Domain=NSURLErrorDomain Code=-1009 "似乎已断开与互联网的连接。" UserInfo=0x1e2ea840 {NSErrorFailingURLStringKey=https://gsp4-cn.ls.apple.com/PlaceSearchServlet, NSErrorFailingURLKey=https://gsp4-cn.ls.apple.com/PlaceSearchServlet, NSLocalizedDescription=似乎已断开与互联网的连接。, NSUnderlyingError=0x1e1172c0 "似乎已断开与互联网的连接。"}

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

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

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

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

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

 

MKReverseGeocoder的初始化,很简单。

 

MKReverseGeocoder *geocoder = [[MKReverseGeocoder allocinitWithCoordinate:currentCoordinate2D];

      geocoder.delegate = self;

[geocoder start];

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

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

}

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

}

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

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

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

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

@property (nonatomicreadonlyNSDictionary *addressDictionary;

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

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

{

    City = "\U897f\U5b89\U5e02";// 城市名字

    Country = "\U4e2d\U56fd";// 国家名字

    CountryCode = CN;// 国家编码

    FormattedAddressLines =     (

        "\U4e2d\U56fd",

        "\U9655\U897f\U7701\U897f\U5b89\U5e02\U96c1\U5854\U533a",

        "\U9ad8\U65b0\U516d\U8def34\U53f7"

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

    State = "\U9655\U897f\U7701"; // 

    Street = "\U9ad8\U65b0\U516d\U8def 34\U53f7";// 街道完整名称

    SubLocality = "\U96c1\U5854\U533a";//区名

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

    Thoroughfare = "\U9ad8\U65b0\U516d\U8def";//街道名称

}

 

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

 

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

// address dictionary properties

@property (nonatomicreadonlyNSString *name; // eg. Apple Inc.

@property (nonatomicreadonlyNSString *thoroughfare; // street address, eg. 1 Infinite Loop

@property (nonatomicreadonlyNSString *subThoroughfare; // eg. 1

@property (nonatomicreadonlyNSString *locality; // city, eg. Cupertino

@property (nonatomicreadonlyNSString *subLocality; // neighborhood, common name, eg. Mission District

@property (nonatomicreadonlyNSString *administrativeArea; // state, eg. CA

@property (nonatomicreadonlyNSString *subAdministrativeArea; // county, eg. Santa Clara

@property (nonatomicreadonlyNSString *postalCode; // zip code, eg. 95014

@property (nonatomicreadonlyNSString *ISOcountryCode; // eg. US

@property (nonatomicreadonlyNSString *country; // eg. United States

@property (nonatomicreadonlyNSString *inlandWater; // eg. Lake Tahoe

@property (nonatomicreadonlyNSString *ocean; // eg. Pacific Ocean

@property (nonatomicreadonlyNSArray *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);

     }];


方法2: 利用google地图api

//根据经纬度 查找地址

- (void)getAddress:(NSString *)aLongitude withLatitude:(NSString *)aLatitude withBlock:(void(^)(id result))callback {

    callbackBlock = callback;

    NSString *url = @"http://maps.googleapis.com/maps/api/geocode/json";

    NSString *coordinateStr = [NSString stringWithFormat:@"%@,%@",aLatitude,aLongitude];

    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];

    [params setValue:@"true" forKey:@"sensor"];

    [params setValue:@"cn" forKey:@"language"];

    [params setObject:coordinateStr forKey:@"latlng"];

    NSString *tempParamStr = [self addParametersToRequest:params];

    NSString *tempUrl = [NSString stringWithFormat:@"%@?%@",url,tempParamStr];

    NSMutableURLRequest *operationRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:tempUrl]];

    

    NSURLConnection *operationConnection = [[NSURLConnection alloc] initWithRequest:operationRequest delegate:self startImmediately:NO];

    [operationConnection start];

}



- (NSString *)addParametersToRequest:(NSMutableDictionary*)parameters {

    NSMutableArray *paramStringsArray = [NSMutableArray arrayWithCapacity:[[parameters allKeys] count]];

    

    for(NSString *key in [parameters allKeys]) {

        NSObject *paramValue = [parameters valueForKey:key];

		if ([paramValue isKindOfClass:[NSString class]]) {

			[paramStringsArray addObject:[NSString stringWithFormat:@"%@=%@", key, [(NSString *)paramValue encodedURLParameterString]]];

		} else {

			[paramStringsArray addObject:[NSString stringWithFormat:@"%@=%@", key, paramValue]];

		}

    }

    

    NSString *paramsString = [paramStringsArray componentsJoinedByString:@"&"];

    return paramsString;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值