#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate,MKMapViewDelegate>
@property (nonatomic, strong) CLLocationManager *manager;
@property (nonatomic, strong) MKMapView *mapView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initMapView];
NSArray *arr = @[@"移动",@"回到用户位置"];
for (NSInteger i = 0; i < arr.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100*i, 100, 100, 30);
[button setTitle:arr[i] forState:UIControlStateNormal];
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 100+i;
[self.view addSubview:button];
}
[self createLongPress];
}
- (void)btnClick:(UIButton *)button {
switch (button.tag) {
case 100:
{
CLLocationCoordinate2D coordinate = self.mapView.region.center;
coordinate.latitude += 0.01;
coordinate.longitude += 0.01;
[self.mapView setCenterCoordinate:coordinate animated:YES];
}
break;
case 101:
{
CLLocation *location = self.mapView.userLocation.location;
[self.mapView setCenterCoordinate:location.coordinate animated:YES];
}
break;
default:
break;
}
}
#pragma mark - 定位
- (void)initManager {
if (self.manager) {
return;
}
self.manager = [[CLLocationManager alloc] init];
#ifdef __IPHONE_8_0 //判断版本
if ([self.manager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.manager performSelector:@selector(requestAlwaysAuthorization)];
}
self.manager.delegate = self;
#endif
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
}
- (void)initMapView {
[self initManager];
self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
self.mapView.mapType = MKMapTypeStandard;
self.mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(34.77274892, 113.67591140),MKCoordinateSpanMake(0.01, 0.01));
self.mapView.showsUserLocation = YES;
self.mapView.delegate = self;
[self.view addSubview:self.mapView];
[self createAnnotation];
}
#pragma mark - 创建点标注
- (void)createAnnotation {
MKPointAnnotation *annotation1 = [[MKPointAnnotation alloc] init];
annotation1.coordinate = CLLocationCoordinate2DMake(34.77274892, 113.67591140);
annotation1.title = [NSString stringWithFormat:@"%f", annotation1.coordinate.latitude];
annotation1.subtitle = [NSString stringWithFormat:@"%f", annotation1.coordinate.longitude];
[self.mapView addAnnotation:annotation1];
MKPointAnnotation *annotation2 = [[MKPointAnnotation alloc] init];
annotation2.coordinate = CLLocationCoordinate2DMake(34.77274892, 113.67591140+0.003);
annotation2.title = [NSString stringWithFormat:@"%f", annotation2.coordinate.latitude];
annotation2.subtitle = [NSString stringWithFormat:@"%f", annotation2.coordinate.latitude];
[self.mapView addAnnotation:annotation2];
}
#pragma mark - MapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKPointAnnotation class]]) {
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"MKPinAnnotationView"];
if (pinView == nil) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MKPinAnnotationView"];
}
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
pinView.pinColor = MKPinAnnotationColorPurple;
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
redView.backgroundColor = [UIColor redColor];
pinView.leftCalloutAccessoryView = redView;
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
button.frame = CGRectMake(0, 0, 30, 30);
pinView.rightCalloutAccessoryView = button;
return pinView;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
NSLog(@"气泡附件被点击");
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
NSLog(@"大头针视图被点击/选中");
}
#pragma mark - 长按添加点标注
- (void)createLongPress {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.mapView addGestureRecognizer: longPress];
}
- (void)longPress:(UILongPressGestureRecognizer *)press {
if (press.state == UIGestureRecognizerStateEnded) {
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
CGPoint point = [press locationInView:self.mapView];
annotation.coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
annotation.title = [NSString stringWithFormat:@"%f", annotation.coordinate.latitude];
annotation.subtitle = [NSString stringWithFormat:@"%f", annotation.coordinate.longitude];
[self.mapView addAnnotation:annotation];
}
}