zoukankan      html  css  js  c++  java
  • IOS获取当前地理位置文本

    本文转载至  http://blog.csdn.net/lvxiangan/article/details/28101119

     

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

    方法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 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 allocinit];

       

        [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;
    
    }
  • 相关阅读:
    【刷题】BZOJ 4059 [Cerc2012]Non-boring sequences
    【刷题】BZOJ 3745 [Coci2015]Norma
    【刷题】BZOJ 2734 [HNOI2012]集合选数
    【刷题】BZOJ 2287 【POJ Challenge】消失之物
    【刷题】BZOJ 2151 种树
    【刷题】BZOJ 2134 单选错位
    【刷题】BZOJ 1924 [Sdoi2010]所驼门王的宝藏
    【刷题】BZOJ 1823 [JSOI2010]满汉全席
    【刷题】BZOJ 1124 [POI2008]枪战Maf
    编程之美 ---> 1.2中国象棋将帅问题
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/3851912.html
Copyright © 2011-2022 走看看