iOS定位经纬度,解码所在城市
首先要添加库CoreLocation.framework
然后需要在info.plist里添加两项NSLocationWhenInUseUsageDescription和NSLocationAlwaysUseUsageDescription后边的value随便写点就行
这是询问当前设备是否可以访问位置权限,
//
// ViewController.m
// Location
//
// Created by wupeige on 15/12/29.
// Copyright © 2015年 wupeige. All rights reserved.
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interfaceViewController ()<CLLocationManagerDelegate>
@property (strong,nonatomic) CLLocationManager *locationManager; //定位管理器
@property (strong,nonatomic) CLGeocoder *geo; //可以根据经度和纬度,解码所在位置
@property (strong,nonatomic) UILabel *lab;
@property (strong,nonatomic) UILabel *lab2;
@property (strong,nonatomic) UILabel *lab3;
@end
@implementation ViewController
@synthesize lab,lab2,lab3;
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
lab = [[UILabelalloc] initWithFrame:CGRectMake(100, 100, 300, 50)];
lab.text = @"经度";
[self.viewaddSubview:lab];
lab2 = [[UILabelalloc] initWithFrame:CGRectMake(100, 200, 300, 50)];
lab2.text = @"纬度";
[self.viewaddSubview:lab2];
lab3 = [[UILabelalloc] initWithFrame:CGRectMake(100, 400, 300, 50)];
lab3.text = @"城市";
[self.viewaddSubview:lab3];
UIButton *btn = [[UIButtonalloc] initWithFrame:CGRectMake(150, 50, 100, 50)];
[btn setTitle:@"定位"forState:UIControlStateNormal];
[btn setTitleColor:[UIColorredColor] forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(Location:) forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn];
}
-(void) Location:(id) sender
{
// 判断是否可用定位服务
if ([CLLocationManagerlocationServicesEnabled]) {
NSLog(@"Star location");
//设置定位精度 最佳精度
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//设置距离过滤器为10米,表示每移动10米,更新一次数据
self.locationManager.distanceFilter = 50;
self.locationManager = [[CLLocationManageralloc] init];
self.geo = [[CLGeocoderalloc] init];
self.locationManager.delegate = self;
//访问是否可以定位 对应info.plist的文件
if ([self.locationManagerrespondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManagerrequestWhenInUseAuthorization];
[self.locationManagerrequestAlwaysAuthorization];
}
//开始监听位置
[self.locationManagerstartUpdatingLocation];
}
else
{
NSLog(@"UNSUPPORTED");
}
}
#pragma mark CLLocationManagerDelegate
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations
{
NSLog(@"获取数据啊");
//获取最后一个定位数据
CLLocation *location = [locations lastObject];
//依次获取 经度,纬度,高度,速度,方向等
lab2.text = @"ddd";
lab.text = [NSStringstringWithFormat:@"%f",location.coordinate.longitude];
lab2.text = [NSStringstringWithFormat:@"%f",location.coordinate.latitude];
__blockNSString *cityName = [[NSStringalloc] init];
//block 回调,在外边拿数据的时候得等回调之后才能拿到,所以对数据的操作写在block回调里
[self.georeverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *place = placemarks[0];
NSLog(@"%@",place.addressDictionary[@"State"]);
cityName = place.addressDictionary[@"State"];
cityName = [cityName substringToIndex:cityName.length - 1];
// NSLog(@"%@",cityName);
lab3.text = [NSStringstringWithFormat:@"%@",cityName];
}];
}
////实时更新数据 立马执行的
//-(void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
//{
//
//}
-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Failed");
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

浙公网安备 33010602011771号