iOS 获取当前地理位置文本
来源:互联网 发布:阿里云企业网站 编辑:程序博客网 时间:2024/04/28 06:03
以下内容摘抄自网络,著作权属于原作者
方法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 “似乎已断开与互联网的连接。”}
发现之前的地图获取当前地理位置信息在Deprecated in iOS 5.0。已经被苹果弃之不用了。
推荐使用CLGeocoder来替代。发现非常简单,比之前写的方法简单了不少。
地图的前提是你导入了MapKit这个库
#import <MapKit/MKMapView.h>先声明一个全局的CLLocationManager对象。 CLLocationManager *_currentLoaction;之后开启定位功能。 _currentLoaction = [[CLLocationManager alloc] init]; _currentLoaction.delegate = self; [_currentLoaction startUpdatingLocation];定位结束之后更新当前的地址经纬度等信息。#pragma mark - Location - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"locError:%@", error);}- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { [_currentLoaction stopUpdatingLocation]; NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude]; NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude]; NSLog(@"Lat: %@ Lng: %@", strLat, strLng); [_geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { _placeMark = [placemarks objectAtIndex:0]; _locationLabel.text = _placeMark.administrativeArea; ITTDINFO(@"%@",_locationLabel.text); // we have received our current location, so enable the "Get Current Address" button }];}
我稍微解释一下
_geocoder 这个是我先要声明的CLGeocoder。使用之前要alloc,才能使用。
我刚开始犯了一个低级错误,没有在viewDidLoad方法中_geocoder = [[CLGeocoder alloc] init];
导致一直nil无法出现block的方法。获取当前所在地的地理位置信息需要使用一个新的类,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 = "\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 (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); }];
方法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;}
- IOS获取当前地理位置文本
- IOS获取当前地理位置文本
- IOS获取当前地理位置文本
- iOS 获取当前地理位置文本
- iOS获取当前地理位置文本
- ios 实现获取当前地理位置
- iOS --- 使用CoreLocation获取当前所在城市等地理位置信息
- IOS学习笔记-获取当前的地理位置swift版
- iOS获取当前设备的型号、分辨率、运营商以及地理位置
- 网页JS获取当前地理位置
- 获取当前网络和地理位置
- js,html获取当前地理位置
- JS获取当前地理位置方法
- ios获取地理位置信息
- ios 8 获取地理位置
- ios 地理位置获取
- ios 简单获取地理位置信息
- Android 使用GPS获取当前地理位置
- 数据库多表连接查询
- NYOJ 会场安排问题 14
- poj 2967 Triangles 排序
- Hibernate根据数据库表生成持久化类和映射文件
- Android客户端制作之连接URL
- iOS 获取当前地理位置文本
- sgu-234 Black-White King Strikes Back
- svn配置
- 贪吃蛇游戏,带声音带彩色,标准C++
- c与c++输入输出二进制文件的异同
- MRtmpLib:rtmp客户端协议实现之rtmp客户端实现(五)
- unity与ANDroid交互
- C++ 经典面试题(一)
- Swift学习小结之泛型