#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];
}
self.mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
self.mapView.delegate = self;
self.mapView.showsTraffic = 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
{
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString *ID = @"annoView";
MKPinAnnotationView *annoView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
if (annoView == nil) {
annoView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
annoView.pinTintColor = [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1];
annoView.animatesDrop = YES;
}
return annoView;
}
#pragma mark - 切換地圖型別
- (IBAction)mapTypeChangeClick:(UISegmentedControl *)sender {
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];
}
@end