欢迎使用CSDN-markdown编辑器

1 篇文章 0 订阅

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值