#import <MapKit/MapKit.h>@interface ViewController () <MKMapViewDelegate>/** 地图显示类 */@property (nonatomic, strong) MKMapView *mapView;@end@implementation ViewController#pragma mark - 懒加载- (MKMapView *)mapView {if (_mapView == nil) {MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];CLLocationCoordinate2D coordinate = {39.9087607478,116.3975780499};MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);[mapView setRegion:region];mapView.delegate = self;// 获取用户当前位置mapView.showsUserLocation = YES;[self.view addSubview:mapView];_mapView = mapView;}return _mapView;}/*** 地理编码和地理反编码* 1. 地理编码根据具体地理信息编码为经纬度* 2. 地理反编码 根据经纬度反编码为地理位置*/- (void)viewDidLoad {[super viewDidLoad];[self mapView];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {[self startSearch];}#pragma mark - 搜索/*** 开始搜索*/- (void)startSearch {// 移除所有的标注[self.mapView removeAnnotations:self.mapView.annotations];/**返回一个范围 1个经纬度 1个经度距离 1个纬度距离即根据中心点的经纬度和 经纬度距离中心点的距离*//* MKCoordinateRegionMakeWithDistance(<#CLLocationCoordinate2D centerCoordinate#>, <#CLLocationDistance latitudinalMeters#>, <#CLLocationDistance longitudinalMeters#>)*/// MKLocalSearch 本地搜索类// 1. 创建请求对象MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];// @property (nonatomic, copy) NSString *naturalLanguageQuery;// @property (nonatomic, assign) MKCoordinateRegion region;// 关键字request.naturalLanguageQuery = @"大学";// 搜索范围request.region = self.mapView.region;// 2. 搜索服务类MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {if (error == nil) {// 搜索的地理信息们 是一个MKMapItem数组 每个MKMapItem里面都有很多地理信息NSArray *mapItems = response.mapItems;for (MKMapItem *mapItem in mapItems) {// 取到地理信息给大头针标注 将大头针标注添加到mapView上,任务完成// 大头针MKPointAnnotation *pointAno = [[MKPointAnnotation alloc] init];pointAno.coordinate = mapItem.placemark.coordinate;pointAno.title = mapItem.name;// 添加标注[self.mapView addAnnotation:pointAno];}}}];}#pragma mark - MKMapView 代理方法- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {// 如果是用户当前位置 不定制标注if ([annotation isKindOfClass:[MKUserLocation class]]) {return nil;}MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"annotation"];if (pin == nil) {pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"];pin.canShowCallout = YES;pin.animatesDrop = YES;}return pin;}/*** 添加覆盖物的时候会触发 只要一调用addOverlay就会调到这里来*/- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {// 覆盖物有三种/*** 1. MKCircle 圆形覆盖物* 2. MKPolygon 多边形覆盖物* 3. MKPolyline 线的覆盖物*/if ([overlay isKindOfClass:[MKCircle class]]) {// MKCircleRenderer -> MKOverlayPathRenderer -> MKOverlayRendererMKCircleRenderer *circleRenderer = [[MKCircleRenderer alloc] initWithCircle:overlay];// 线条颜色circleRenderer.strokeColor = [UIColor redColor];// 线条宽度circleRenderer.lineWidth = 5.0f;// 填充颜色circleRenderer.fillColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3]; // 颜色的设置 带着透明度return circleRenderer;}return nil;}@end
700字范文,内容丰富有趣,生活中的好帮手!