1.在Info.plist 添加下面两个
NSLocationAlwaysUsageDescription //NS位置总是使用描述
NSLocationWhenInUseUsageDescription //NS位置在使用使用描述
2.在工程里“Build Phases”—> Link Binary With Libraries 添加 MapKit.framework
3.main.storyboard 里添加 MapKit View视图
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h> //1
#import <MapKit/MapKit.h> //11
@interface ViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>
{
CLLocationManager *_locationMgr; //1
CLGeocoder *_geocoder; //地理位置编码与反编码 34
}
@property (weak, nonatomic) IBOutlet MKMapView *mapView; //10
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//35
_geocoder = [[CLGeocoder alloc]init];
//创建对象 1
_locationMgr = [[CLLocationManager alloc]init];
//定位精度, 精度越高,定位所需时间越长 // 2 kCLLocationAccuracyBest 定位精度最好的
_locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
//重新定位所需移动的最小距离,None表示任意距离都会导致重新定位 / 3
_locationMgr.distanceFilter = kCLDistanceFilterNone;
//添加_locationMgr的代理CLLocationManagerDelegate
_locationMgr.delegate = self; //4
//开始更新地理位置 5 locationServicesEnabled (启用定位服务)
if ([CLLocationManager locationServicesEnabled]) {
//请求用户授权 9
[_locationMgr requestAlwaysAuthorization];
//startUpdatingLocation 开始更新位置 6
[_locationMgr startUpdatingLocation];
}
else
{
NSLog(@"location serivce not enabled!");
}
_mapView.delegate = self;
}
#pragma mark - mapView
//将标注显示出来 addAnnotation时, 会调用该方法 19
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *indentifer = @"anno"; //20
if (_mapView.userLocation != annotation) {
//不是用户当前位置则创建
MKAnnotationView *pinAnno = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:indentifer]; //21
if (pinAnno == nil) {
pinAnno = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:indentifer]; //22
//从天而降的动画
// pinAnno.animatesDrop = YES;//23
// pinAnno.pinColor = MKpinAnnotationColorPurple; //24
pinAnno.image = [UIImage imageNamed:@"icon_route_start"]; //25
pinAnno.canShowCallout = YES; //26
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 44, 44)]; //27
imgView.image = [UIImage imageNamed:@"01"]; //28
//
//给标注设置左弹出图片 //29
pinAnno.leftCalloutAccessoryView = imgView;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoLight];
//给标注添加按钮 32
pinAnno.rightCalloutAccessoryView = btn;
}
return pinAnno; //23
}
else
{
CLLocationCoordinate2D userLocation = _mapView.userLocation.location.coordinate;
//打印当前的经度与维度
NSLog(@"lat:%f, long:%f",userLocation.latitude,userLocation.longitude);
return nil;
}
}
#pragma mark - MKMapViewDelegate
//33
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control //control就是上面创建的button
{
NSLog(@"control:%@", control);
}
//加载地图地图视图并失败 时调用 13
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
NSLog(@"%s",__func__);
}
// 地图视图并完成加载地图 14
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
NSLog(@"%s",__func__);
}
#pragma mark -CLLocationManagerDelegate
//获取地理位置时,会调用该方法 didUpdateLocations (并更新位置) 7
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"locations:%@",locations);
[_locationMgr stopUpdatingLocation]; //停止定位10
CLLocation *location = locations[0]; //获取一个位置 12
CLLocationCoordinate2D coordinate = location.coordinate; //13
//让地图定位到指定的点 , span是地图的缩放级别
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);//14
MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);
[_mapView setRegion:region animated:YES]; //15
// //给地图添加标注
// MKPointAnnotation *pointAnno = [[MKPointAnnotation alloc]init]; //16
//
// pointAnno.coordinate = coordinate; //给标注指定经纬度 17
//
// //如果不设置title,则无法弹出气泡信息框
// pointAnno.title = @"title"; //30
// pointAnno.subtitle = @"subtitle"; //31
//
// [_mapView addAnnotation:pointAnno]; //将标注加入地图 18
//地理位置反编码,将经纬度信息转换为真实的位置 36
[_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
// NSLog(@"place:%@", placemarks);
if (error) {
NSLog(@"error:%@", error); //37
}
CLPlacemark *clpk = placemarks[0]; //38 获取位置信息
NSLog(@"clpk:%@",clpk.addressDictionary);
NSLog(@"city:%@", clpk.addressDictionary[@"City"]);
NSLog(@"street:%@", clpk.addressDictionary[@"Street"]);
//给地图添加标注
MKPointAnnotation *pointAnno = [[MKPointAnnotation alloc]init]; //16
pointAnno.coordinate = coordinate; //给标注指定经纬度 17
//如果不设置title,则无法弹出气泡信息框
pointAnno.title = [NSString stringWithFormat:@"%@",clpk.addressDictionary[@"City"]]; //30
pointAnno.subtitle = [NSString stringWithFormat:@"%@",clpk.addressDictionary[@"Street"]]; //31
[_mapView addAnnotation:pointAnno]; //将标注加入地图 18
}];
}
//获取地理位置出错时,调用此方法 didFailWithError 8
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
#import <CoreLocation/CoreLocation.h> //1
#import <MapKit/MapKit.h> //11
@interface ViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>
{
CLLocationManager *_locationMgr; //1
CLGeocoder *_geocoder; //地理位置编码与反编码 34
}
@property (weak, nonatomic) IBOutlet MKMapView *mapView; //10
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//35
_geocoder = [[CLGeocoder alloc]init];
//创建对象 1
_locationMgr = [[CLLocationManager alloc]init];
//定位精度, 精度越高,定位所需时间越长 // 2 kCLLocationAccuracyBest 定位精度最好的
_locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
//重新定位所需移动的最小距离,None表示任意距离都会导致重新定位 / 3
_locationMgr.distanceFilter = kCLDistanceFilterNone;
//添加_locationMgr的代理CLLocationManagerDelegate
_locationMgr.delegate = self; //4
//开始更新地理位置 5 locationServicesEnabled (启用定位服务)
if ([CLLocationManager locationServicesEnabled]) {
//请求用户授权 9
[_locationMgr requestAlwaysAuthorization];
//startUpdatingLocation 开始更新位置 6
[_locationMgr startUpdatingLocation];
}
else
{
NSLog(@"location serivce not enabled!");
}
_mapView.delegate = self;
}
#pragma mark - mapView
//将标注显示出来 addAnnotation时, 会调用该方法 19
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *indentifer = @"anno"; //20
if (_mapView.userLocation != annotation) {
//不是用户当前位置则创建
MKAnnotationView *pinAnno = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:indentifer]; //21
if (pinAnno == nil) {
pinAnno = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:indentifer]; //22
//从天而降的动画
// pinAnno.animatesDrop = YES;//23
// pinAnno.pinColor = MKpinAnnotationColorPurple; //24
pinAnno.image = [UIImage imageNamed:@"icon_route_start"]; //25
pinAnno.canShowCallout = YES; //26
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 44, 44)]; //27
imgView.image = [UIImage imageNamed:@"01"]; //28
//
//给标注设置左弹出图片 //29
pinAnno.leftCalloutAccessoryView = imgView;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoLight];
//给标注添加按钮 32
pinAnno.rightCalloutAccessoryView = btn;
}
return pinAnno; //23
}
else
{
CLLocationCoordinate2D userLocation = _mapView.userLocation.location.coordinate;
//打印当前的经度与维度
NSLog(@"lat:%f, long:%f",userLocation.latitude,userLocation.longitude);
return nil;
}
}
#pragma mark - MKMapViewDelegate
//33
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control //control就是上面创建的button
{
NSLog(@"control:%@", control);
}
//加载地图地图视图并失败 时调用 13
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
NSLog(@"%s",__func__);
}
// 地图视图并完成加载地图 14
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
NSLog(@"%s",__func__);
}
#pragma mark -CLLocationManagerDelegate
//获取地理位置时,会调用该方法 didUpdateLocations (并更新位置) 7
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"locations:%@",locations);
[_locationMgr stopUpdatingLocation]; //停止定位10
CLLocation *location = locations[0]; //获取一个位置 12
CLLocationCoordinate2D coordinate = location.coordinate; //13
//让地图定位到指定的点 , span是地图的缩放级别
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);//14
MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);
[_mapView setRegion:region animated:YES]; //15
// //给地图添加标注
// MKPointAnnotation *pointAnno = [[MKPointAnnotation alloc]init]; //16
//
// pointAnno.coordinate = coordinate; //给标注指定经纬度 17
//
// //如果不设置title,则无法弹出气泡信息框
// pointAnno.title = @"title"; //30
// pointAnno.subtitle = @"subtitle"; //31
//
// [_mapView addAnnotation:pointAnno]; //将标注加入地图 18
//地理位置反编码,将经纬度信息转换为真实的位置 36
[_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
// NSLog(@"place:%@", placemarks);
if (error) {
NSLog(@"error:%@", error); //37
}
CLPlacemark *clpk = placemarks[0]; //38 获取位置信息
NSLog(@"clpk:%@",clpk.addressDictionary);
NSLog(@"city:%@", clpk.addressDictionary[@"City"]);
NSLog(@"street:%@", clpk.addressDictionary[@"Street"]);
//给地图添加标注
MKPointAnnotation *pointAnno = [[MKPointAnnotation alloc]init]; //16
pointAnno.coordinate = coordinate; //给标注指定经纬度 17
//如果不设置title,则无法弹出气泡信息框
pointAnno.title = [NSString stringWithFormat:@"%@",clpk.addressDictionary[@"City"]]; //30
pointAnno.subtitle = [NSString stringWithFormat:@"%@",clpk.addressDictionary[@"Street"]]; //31
[_mapView addAnnotation:pointAnno]; //将标注加入地图 18
}];
}
//获取地理位置出错时,调用此方法 didFailWithError 8
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"err:%@",error);
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h> //1
#import <MapKit/MapKit.h> //11
@interface ViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>
{
CLLocationManager *_locationMgr; //1
CLGeocoder *_geocoder; //地理位置编码与反编码 34
}
@property (weak, nonatomic) IBOutlet MKMapView *mapView; //10
@end
@implementation ViewController
#import <CoreLocation/CoreLocation.h> //1
#import <MapKit/MapKit.h> //11
@interface ViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>
{
CLLocationManager *_locationMgr; //1
CLGeocoder *_geocoder; //地理位置编码与反编码 34
}
@property (weak, nonatomic) IBOutlet MKMapView *mapView; //10
@end
@implementation ViewController