// 首先建立一个类 接受 <MKAnnotation>
LHAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MKMapView.h>
@interface LHAnnotation : NSObject<MKAnnotation>
// Center latitude and longitude of the annotion view.
// The implementation of this property must be KVO compliant.
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
// Title and subtitle for use by selection UI.
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
// 地图页面
MapViewController.h
#import "ViewController.h"
#import <MapKit/MKMapView.h>
@interface MapViewController : ViewController<CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
MapViewController.m
#import "MapViewController.h"
#import "LHAnnotation.h"
@interface MapViewController ()<MKMapViewDelegate>
@end
@implementation MapViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// UITapGestureRecognizer *mTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPress:)];
UILongPressGestureRecognizer *mTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapPress:)];
[self.mapView addGestureRecognizer:mTap];
}
- (void)tapPress:(UIGestureRecognizer*)gestureRecognizer {
[_mapView removeOverlays:_mapView.overlays];
[_mapView removeAnnotations:_mapView.annotations];
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];//这里touchPoint是点击的某点在地图控件中的位置
CLLocationCoordinate2D touchMapCoordinate =[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];//这里touchMapCoordinate就是该点的经纬度了
NSLog(@"touching %f,%f",touchMapCoordinate.latitude,touchMapCoordinate.longitude);
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
CLLocation *location = [[CLLocation alloc]initWithLatitude:touchMapCoordinate.latitude longitude:touchMapCoordinate.longitude];
__block NSString *name = nil;
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
for (CLPlacemark *place in placemarks) {
NSDictionary *location =[place addressDictionary];
NSLog(@"国家:%@",[location objectForKey:@"Country"]);
NSLog(@"城市:%@",[location objectForKey:@"State"]);
NSLog(@"区:%@",[location objectForKey:@"SubLocality"]);
NSLog(@"位置:%@", place.name);
NSLog(@"国家:%@", place.country);
NSLog(@"城市:%@", place.locality);
NSLog(@"区:%@", place.subLocality);
NSLog(@"街道:%@", place.thoroughfare);
NSLog(@"子街道:%@", place.subThoroughfare);
name = place.name;
LHAnnotation* annotation=[[LHAnnotation alloc]init];
annotation.coordinate=CLLocationCoordinate2DMake(touchMapCoordinate.latitude, touchMapCoordinate.longitude);
annotation.title=name;
// annotation.subtitle=@"123";
[self.mapView addAnnotation:annotation];
}
}];
}
-(void)setMap
{
self.mapView=[[MKMapView alloc]init];
self.mapView.frame=self.view.bounds;
/**
*mapType
*
* MKMapTypeStandard = 0,
MKMapTypeSatellite,
MKMapTypeHybrid
*/
self.mapView.mapType=MKMapTypeStandard;
/**
*Set to YES to add the user location annotation to the map and start updating its location
*/
self.mapView.showsUserLocation=NO;
/**
*Zoom and scroll are enabled by default.
*/
self.mapView.scrollEnabled=YES;
self.mapView.zoomEnabled=YES;
/**
*Rotate and pitch are enabled by default on Mac OS X and on iOS 7.0 and later.
*/
self.mapView.pitchEnabled=YES;
self.mapView.rotateEnabled=YES;
[self.view addSubview:self.mapView];
}