1、先导入 本地动态库 MapKit.framework
2、新建CusstomAnnotation继承于 NSObject
//记得导入import
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface CusstomAnnotation : NSObject<MKAnnotation>
@property (nonatomic)CLLocationCoordinate2D coordinate;
@property (nonatomic,copy)NSString *title;
@property (nonatomic,copy)NSString *subtitle;
@end
3、在 ViewConroller 里面
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "CusstomAnnotation.h"
@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
//创建位置信息管理对象 定义一个全局的变量用来记录CLLocationManager对象
@property (nonatomic,strong) CLLocationManager *locationManager;
@property (nonatomic,strong)MKMapView *mapView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//when-in-use和 Always 都是iOS8之后出现的方法,如果不进行版本适配,运行在iOS7上就会crash
if ([[UIDevice currentDevice].systemVersion floatValue] >=8.0 ) {
[_locationManager requestAlwaysAuthorization];
}
_locationManager = [CLLocationManager new];
//地图显示的位置精度
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//没100米更新位置信息
_locationManager.distanceFilter = 100;
if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
//永久授权
[_locationManager requestAlwaysAuthorization];
//使用中授权
[_locationManager requestWhenInUseAuthorization];
}
//更新位置信息
[_locationManager startUpdatingLocation];
//设置代理
_locationManager.delegate = self;
[self.view addSubview:self.mapView];
_mapView.delegate = self;
[self addAnnotation];
}
#pragma mark --CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
CLLocation *loaction =[locations lastObject];
CLLocationCoordinate2D coordinate = [loaction coordinate];
/*
1.coordinate:当前位置的坐标
latitude 纬度
longitude 经度
2.altitude:海拔,高度
3.horizontalAccuracy:纬度和经度的精度
4.vericalAccuracy:垂直精度(获取不到海拔时为负数)
5.course:行进方向(真北)
6.speed:以米/秒为单位的速度
7.description:位置描述信息
*/
NSLog(@"%.3f,%.3f",coordinate.latitude,coordinate.longitude);
MKCoordinateSpan span = {0.1,0.1};
MKCoordinateRegion region = {coordinate,span};
[self.mapView setRegion:region animated:YES];
}
- (void)addAnnotation
{
CLLocationCoordinate2D coordinate = {22.28,114.15};
CusstomAnnotation *cusston = [CusstomAnnotation new];
cusston.coordinate = coordinate;
cusston.title = @"xxxx";
cusston.subtitle = @"哎哟,我擦";
[self.mapView addAnnotation:cusston];
}
//当访问位置服务失败的情况下 会调用该协议方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
if ([error code] == kCLErrorDenied) {
//访问被拒绝
NSLog(@"访问被拒绝");
}
if ([error code]== kCLErrorLocationUnknown) {
NSLog(@"无法取得位置信息");
}
}
#pragma mark --MKMapViewDelegate
//创建 annotation
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *indetifier = @"annotation";
MKPinAnnotationView *pinAnnotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:indetifier];
if (!pinAnnotationView) {
pinAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:indetifier];
pinAnnotationView.animatesDrop = YES;
pinAnnotationView.pinTintColor = [UIColor redColor];
pinAnnotationView.canShowCallout = YES;
}
return pinAnnotationView;
}
#pragma mark - getter setter
//初始化mapView
- (MKMapView *)mapView
{
if (!_mapView) {
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
_mapView.showsBuildings = YES;
_mapView.showsUserLocation = YES;
_mapView.mapType = MKMapTypeStandard
; //MKMapTypeSatellite 卫星地图
}
return _mapView;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end