MapKit&CoreLocation基本使用以及地圖大頭針的新增與個性化

liushaozhuanyong發表於2017-08-07
//
//  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

相關文章