从零开始系列之iOS地图获取当前城市
2015-06-02 16:50
435 查看
发现之前的地图获取当前地理位置信息在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的方法。
相关文章推荐
- 在Google 地图上实现做的标记相连接
- PHP计算百度地图两个GPS坐标之间距离的方法
- sogou地图API用法实例教程
- gps各种地图坐标系转换
- [IOS地图开发系类]1、CLLocationManager 位置定位
- [IOS地图开发系类]2、位置解码CLGeocoder
- [IOS地图开发系类]3、地图视图MKMapview
- [IOS地图开发系类]4、MKMapview上添加MKAnnotationView,标记当前位置
- [IOS地图开发系类]5、改变大头针MKPinAnnotationView的颜色
- 魔兽地图修改器
- 国家测绘局副局长解释什么是“问题地图”ぁ
- 地图 点聚合
- asp.net地理统计图表控件
- Yahoo前端优化性能规则
- ADS-B雷达显示终端 5.6
- ios 地图大头针自定义显示图片 MKAnnotationView
- 根据经纬度获得城市
- 3D地图
- iOS使用Google Map Api创建地图-显示标注-轨迹回放
- 计算两地距离多少公里或者英里