MapKit&CoreLocation基本使用以及地图大头针的添加与个性化

//
//  MRMapViewController.m
//  CoreLocation&MapKit
//
//  Created by Mr.Robot on 2017/8/6.
//  Copyright © 2017年 Mr.Robot. All rights reserved.
//

#import "MRMapViewController.h"
#import "MyAnnotationModel.h"

#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface MRMapViewController () <MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) CLLocationManager *mgr;

@end

@implementation MRMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.mgr = [CLLocationManager new];

    if ([self.mgr respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.mgr requestAlwaysAuthorization];
    }



    /**
     userTrackingMode几种选择,以及意义:
     MKUserTrackingModeNone = 0, // the user's location is not followed(不跟随用户地点)
     MKUserTrackingModeFollow, // the map follows the user's location(跟随用户位置)
     MKUserTrackingModeFollowWithHeading __TVOS_PROHIBITED, // the map follows the user's location and heading(跟随用户位置并用箭头指向用户朝向)
     */
    self.mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
    //设置代理
    self.mapView.delegate = self;

    /**
     ios9新特性
     */
    //显示交通状况
    self.mapView.showsTraffic = YES;

    //设置指南针(默认就是yes)
    self.mapView.showsCompass = YES;

    //设置比例尺
    self.mapView.showsScale = YES;
}

#pragma mark - 用户点击地图即添加大头针
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self.mapView];

    CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];

    MyAnnotationModel *annotationModel = [MyAnnotationModel new];

    annotationModel.coordinate = coordinate;
    annotationModel.title = @"";
    annotationModel.subTitle = @"";

    [self.mapView addAnnotation:annotationModel];
}

//自定义大头针模型
#pragma mark - 只要添加了大头针模型就会调用这个方法,返回对应的View
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{

    //如果返回nil, 就代表用户没有自定义的需求, 所有的View样式由系统处理
    //MKUserLocation: 系统专门显示用户位置的大头针模型
    //MyAnnotationModel: 自定义的类

    //1. 如果发现是显示用户位置的大头针模型,  就返回nil
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }


    //2. 自定义大头针View --> 跟Cell的创建几乎一样
    static NSString *ID = @"annoView";

    //MKAnnotationView : 默认image属性没有赋值
    //MKPinAnnotationView : 子类是默认有View的
    MKPinAnnotationView *annoView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];

    if (annoView == nil) {
        annoView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];

        /**
         MKPinAnnotationColorRed
         MKPinAnnotationColorGreen,
         MKPinAnnotationColorPurple
         */

        // 设置颜色 iOS9首次过期
        //annoView.pinColor =  MKPinAnnotationColorGreen;

        //3. 设置颜色, iOS9新增
        annoView.pinTintColor = [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0  blue:arc4random_uniform(256) / 255.0  alpha:1];

        //4. 设置动画掉落
        annoView.animatesDrop = YES;

    }

    return annoView;

}

#pragma mark - 切换地图类型
- (IBAction)mapTypeChangeClick:(UISegmentedControl *)sender {

    /**
     MKMapType类型
     typedef NS_ENUM(NSUInteger, MKMapType) {
     MKMapTypeStandard = 0,   标准
     MKMapTypeSatellite,      卫星
     MKMapTypeHybrid,         混合模式
     MKMapTypeSatelliteFlyover NS_ENUM_AVAILABLE(10_11, 9_0),            卫星模式flyover(flyover模式中国地区不能用)
     MKMapTypeHybridFlyover NS_ENUM_AVAILABLE(10_11, 9_0),               混合模式flyover
     } NS_ENUM_AVAILABLE(10_9, 3_0) __TVOS_AVAILABLE(9_2) __WATCHOS_PROHIBITED;
     */

    switch (sender.selectedSegmentIndex) {
        case 0:
            self.mapView.mapType = MKMapTypeStandard;
            break;
        case 1:
            self.mapView.mapType = MKMapTypeSatellite;
            break;
        case 2:
            self.mapView.mapType = MKMapTypeHybrid;
            break;
        default:
            break;
    }

}

#pragma mark - 定位按钮
- (IBAction)locateClick:(id)sender {

    //获取当前用户位置-以二维坐标表示
    CLLocationCoordinate2D coordinate = self.mapView.userLocation.location.coordinate;

    //获取当前显示范围
    MKCoordinateSpan coordinateSpan = MKCoordinateSpanMake(self.mapView.region.span.latitudeDelta, self.mapView.region.span.longitudeDelta);
    //设置显示范围及中心点地理坐标
    [self.mapView setRegion:MKCoordinateRegionMake(coordinate, coordinateSpan) animated:YES];
}

#pragma mark - 调整地图大小
#pragma mark - 放大
- (IBAction)zoomInClick:(id)sender {
    CGFloat latitudeDelta = self.mapView.region.span.latitudeDelta * 0.5;
    CGFloat longitudeDelta = self.mapView.region.span.longitudeDelta * 0.5;

    [self.mapView setRegion:MKCoordinateRegionMake(self.mapView.centerCoordinate, MKCoordinateSpanMake(latitudeDelta, longitudeDelta)) animated:YES];
}

#pragma mark - 缩小
- (IBAction)zoomOutClick:(id)sender {
    CGFloat latitudeDelta = self.mapView.region.span.latitudeDelta * 2;
    CGFloat longitudeDelta = self.mapView.region.span.longitudeDelta * 2;

    [self.mapView setRegion:MKCoordinateRegionMake(self.mapView.centerCoordinate, MKCoordinateSpanMake(latitudeDelta, longitudeDelta)) animated:YES];
}

- (BOOL)prefersStatusBarHidden
{
    return NO;
}

- (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、付费专栏及课程。

余额充值