IOS 定位和地图

定位

//第一步导入框架

@import CoreLocation;

//协议和属性

@interface LocationViewController ()

@property (nonatomic, strong) CLLocationManager *locationManager;

@property (nonatomic, strong) CLGeocoder *geocoder;

@end

@implementation LocationViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.title = @"定位";

[self startLocation];

[self startCoder];

// Do any additional setup after loading the view.

}

//开始定位

- (void)startLocation

{

if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) {

//设置代理

_locationManager = [[CLLocationManager alloc] init];

_locationManager.delegate = self;

//设置定位精度

_locationManager.desiredAccuracy = kCLLocationAccuracyBest;

//多远定位依次

_locationManager.distanceFilter = 100;

//ios8

if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){

[_locationManager requestAlwaysAuthorization];

}

//启动

[_locationManager startUpdatingLocation];

}else

{

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示!" message:@"需要开启定位服务,请到设置->隐私,打开定位服务" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];

[alertView show];

}

}

#pragma mark - ****************CoreLocation 代理****************

#pragma mark - 跟踪定位代理方法,每次位置发生变化都会执行(只要定位到相应的位置)

//可以通过模拟器设置一个虚拟的位置,否则在模拟器中无法调用此方法

//定位成功

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

CLLocation *location = [locations lastObject];//取出最后一个位置

CLLocationCoordinate2D coordinate = location.coordinate;//位置坐标

NSLog(@"经度:%f,维度:%f",coordinate.longitude,coordinate.latitude);

//如果不需要实时定位,使用完即使关闭定位服务

[_locationManager stopUpdatingLocation];

}

//定位失败

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

}

- (void)startCoder

{

_geocoder = [[CLGeocoder alloc] init];

//编码  通过地址获得经纬度

[self getCoordinateByAddress:@"北京"];

//反编码  通过经纬度获得地址

[self getAddressByLatitude:40.04 longitude:116.34];

}

#pragma mark - ****************编码****************

-(void)getCoordinateByAddress:(NSString *)address{

//地理编码

//1. 初始化编码对象

_geocoder = [[CLGeocoder alloc] init];

//2. 系统的block方法 geocodeAddressString

[_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {

//根据方法取得一组数据信息,取最后一个对象

NSArray *marks = placemarks;

CLPlacemark *placeMark = [marks lastObject];

//通过 placeMark 获取当前Location 对象

//通过 location 对象获取当前的经纬度

CLLocation *location = placeMark.location;

NSLog(@"%@",placeMark.country);

NSLog(@"%f,%f",location.coordinate.latitude,location.coordinate.longitude);

}];

}

#pragma mark - ****************反编码 (根据坐标取得地名)****************

-(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{

//反地理编码

//初始化 location 对象 和 geocoder 对象

CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

_geocoder = [[CLGeocoder alloc] init];

//调用 geocoder 的block方法 reverseGeocodeLocation

[_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

CLPlacemark *placeMark = [placemarks lastObject];

NSDictionary *dict = placeMark.addressDictionary;

NSLog(@"详细信息:%@",dict);

NSLog(@"City:%@",[dict objectForKey:@"City"]);

}];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

#import "MapkitViewController.h"

@import MapKit;

@import CoreLocation;

#import "MYAnnotion.h"

@interface MapkitViewController ()

@property (nonatomic, strong) CLLocationManager *locationManager;

@property (nonatomic, strong) MKMapView *mapView;

@end

@implementation MapkitViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.title = @"地图";

[self initWithGUI];

// Do any additional setup after loading the view.

}

#pragma mark - ****************初始化地图****************

- (void) initWithGUI{

//初始化 mapView

_mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];

[self.view addSubview:_mapView];

//设置代理

_mapView.delegate = self;

//请求定位服务

_locationManager = [[CLLocationManager alloc] init];

if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {

[_locationManager requestAlwaysAuthorization];

}

//地图类型

_mapView.mapType = MKMapTypeStandard;

//用户位置追踪(用户位置追踪用于标记用户当前位置,此时会调用定位服务)

_mapView.userTrackingMode = MKUserTrackingModeFollow;

//添加大头针

[self addAnnotation];

}

#pragma mark 添加大头针

-(void)addAnnotation{

CLLocationCoordinate2D location1=CLLocationCoordinate2DMake(40.0304727852, 116.3434817061);

MYAnnotion *annotation1=[[MYAnnotion alloc]init];

annotation1.title=@"测试";

annotation1.subtitle=@"蓝鸥";

annotation1.coordinate=location1;

[_mapView addAnnotation:annotation1];

}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

{

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

你可能感兴趣的:(IOS 定位和地图)