作者:骑猪猪-逛恋空 | 来源:互联网 | 2023-08-20 16:16
MRMapViewController.mCoreLocation&MapKitCreatedbyMr.Roboton201786.
#import "MRMapViewController.h"
#import "MyAnnotationModel.h"
#import
#import
@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 *)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)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