地图
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController () <CLLocationManagerDelegate, MKMapViewDelegate> {
MKMapView *myMapView;
CLLocationManager *myLocManager;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
myMapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
myMapView.delegate = self;
[self.view addSubview:myMapView];
CLLocationCoordinate2D c2d = CLLocationCoordinate2DMake(30.662221 , 104.041367);
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = MKCoordinateRegionMake(c2d, span);
[myMapView setRegion:region animated:YES];
myLocManager = [[CLLocationManager alloc] init];
myLocManager.delegate = self;
[myLocManager requestAlwaysAuthorization];
[myLocManager startUpdatingLocation];
myMapView.showsUserLocation = YES;
UILongPressGestureRecognizer *r = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(createPin:)];
[myMapView addGestureRecognizer:r];
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
CLLocation *loc = [[CLLocation alloc] initWithLatitude:c2d.latitude longitude:c2d.longitude];
[geoCoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {
if (!error) {
CLPlacemark *pMark = [placemarks firstObject];
NSData *data = [NSJSONSerialization dataWithJSONObject:pMark.addressDictionary options:NSJSONWritingPrettyPrinted error:nil];
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
MKPlacemark *mkMark = [[MKPlacemark alloc] initWithPlacemark:pMark];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:mkMark];
NSDictionary *options = @{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard)};
[mapItem openInMapsWithLaunchOptions:options];
}
else {
NSLog(@"%@", error);
}
}];
}
- (void) createPin:(UILongPressGestureRecognizer *) sender {
if (sender.state == UIGestureRecognizerStateBegan) {
CGPoint point = [sender locationInView:self.view];
CLLocationCoordinate2D c2d = [myMapView convertPoint:point toCoordinateFromView:self.view];
MKPointAnnotation *ann = [[MKPointAnnotation alloc] init];
ann.coordinate = c2d;
ann.title = @"这是一个标注";
ann.subtitle = @"这是标注的说明";
[myMapView addAnnotation:ann];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (id)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN"];
if (!pinView) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PIN"];
}
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
headerImageView.layer.cornerRadius = 20;
headerImageView.layer.masksToBounds = YES;
headerImageView.image = [UIImage imageNamed:@"7.jpg"];
pinView.leftCalloutAccessoryView = headerImageView;
UIButton *goButton = [UIButton buttonWithType:UIButtonTypeCustom];
goButton.frame = CGRectMake(0, 0, 40, 40);
[goButton setTitle:@"▶️" forState:UIControlStateNormal];
[goButton addTarget:self action:@selector(goButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = goButton;
return pinView;
}
- (void) goButtonClicked:(UIButton *) sender {
NSLog(@"OK");
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations firstObject];
MKCoordinateRegion newRegion = MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(0.1, 0.1));
[myMapView setRegion:newRegion animated:YES];
}
@end