1 显示地图
MKMapView MKMapViewDelegate
准备:加载MapKit.framework
设置地图样式 代理-实现代理方法
- (void)viewDidLoad { [super viewDidLoad]; 设置样式,枚举类型MKMapType self.mapView.mapType = MKMapTypeStandard; // self.mapView.mapType = MKMapTypeSatellite; 将当前视图控制器赋值给地图视图的delegate属性 self.mapView.delegate = self; }
- (void)mapViewDidFailLoadingMap:(MKMapView *)theMapView withError:(NSError *)error {
NSLog(@"error : %@",[error description]);
}
2 添加标注
创建地图标注点类:实现MKAnnotation协议
触发添加动作:编码位置信息-移除地图标注点-调整地图位置和缩放比例 MKCoordinateRegion(CLLocationCoordinate2D center;MKCoordinateSpan span;) 并设置显示区域-设置标注点并放到地图上
实现地图委托方法mapView:viewForAnnotation:在地图视图添加标注时回调
触发添加动作- (IBAction)geocodeQuery:(id)sender { if (_txtQueryKey.text == nil || [_txtQueryKey.text length] == 0) { return; } CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:_txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"查询记录数:%lu",[placemarks count]); if ([placemarks count] > 0) {移除标注点 [self.mapView removeAnnotations:self.mapView.annotations]; } for (int i = 0; i < [placemarks count]; i++) { CLPlacemark* placemark = placemarks[i]; //调整地图位置和缩放比例 MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(placemark.location.coordinate, 10000, 10000); [self.mapView setRegion:viewRegion animated:YES]; 自定义的实现了MKAnnotation协议的地图标注点类(气泡信息) MyAnnotation *annotation = [[MyAnnotation alloc] init]; 将编码好的位置信息放到地图标注点中 annotation.streetAddress = placemark.thoroughfare; annotation.city = placemark.locality; annotation.state = placemark.administrativeArea; annotation.zip = placemark.postalCode; annotation.coordinate = placemark.location.coordinate; 将标注点添加到地图视图上 [self.mapView addAnnotation:annotation]; } //关闭键盘 [_txtQueryKey resignFirstResponder]; }]; }
#pragma mark Map View Delegate Methods 实现协议
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation
使用重用机制获取标注视图(annotation和annotationView的区别)
MKPinAnnotationView *annotationView
= (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"];
if(annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"PIN_ANNOTATION"];
}设置大头针标注图颜色
annotationView.pinColor = MKPinAnnotationColorPurple;
设置标注视图是否以动画效果的形式显示在地图上
annotationView.animatesDrop = YES;
设置附加信息气泡,气泡文字封装在MYAnnotation类中
annotationView.canShowCallout = YES;
return annotationView;
}
地图标注点类:
- (NSString *)title {协议方法就是设置title和subtitle return @"您的位置!"; } - (NSString *)subtitle {拼接字符串 NSMutableString *ret = [NSMutableString new]; if (_state) [ret appendString:_state]; if (_city) [ret appendString:_city]; if (_city && _state) [ret appendString:@", "]; if (_streetAddress && (_city || _state || _zip)) [ret appendString:@" • "]; if (_streetAddress) [ret appendString:_streetAddress]; if (_zip) [ret appendFormat:@", %@", _zip]; return ret; }
3 跟踪用户位置变化
MapKit提供了跟踪用户位置和方向变化的API:开启showUserLocation属性并设置方法setUserTrackingMode:就可以了
- (void)viewDidLoad { [super viewDidLoad]; if ([CLLocationManager locationServicesEnabled])判断能否开启定位服务 { self.mapView.mapType = MKMapTypeStandard; self.mapView.delegate = self; //self.mapView.showsUserLocation = YES;允许跟踪用户信息 设置用户跟踪模式 枚举类型 [self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES]; //授权 self.locationManager = [[CLLocationManager alloc] init]; [self.locationManager requestWhenInUseAuthorization]; [self.locationManager requestAlwaysAuthorization]; } }
4 使用程序外地图
只是用地图的基本功能而不需要设计自己的地图界面。
1)调用iOS苹果地图
MKPlacemark:定位使用的地标类,CLPlacemark是地图上的地标类
MKMapItem:封装了地图上一个点的信息类 ,我们将需要需要显示在地图上的点封装到这里,也用这个类调用苹果地图。
- (IBAction)geocodeQuery:(id)sender { if (self.txtQueryKey.text == nil || [self.txtQueryKey.text length] == 0) { return; } CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:self.txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"查询记录数:%lu",[placemarks count]); if ([placemarks count] > 0) { CLPlacemark* placemark = placemarks[0]; 获取地理坐标点 CLLocationCoordinate2D coordinate = placemark.location.coordinate; 获取该地理坐标点的信息 NSDictionary* address = placemark.addressDictionary; 定位地标点设置 MKPlacemark *place = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:address]; 设置地图上点的信息 MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place]; 调用iOS自带的苹果地图应用 参数是字典类型可以控制显示地图的初始化信息 [mapItem openInMapsWithLaunchOptions:nil];如果有多个点需要标注:先遍历再使用+(BOOL)openMapsWithItems:(NSArray *)mapItems launchOptions:(NSDictionary *)lunchOptions // //地图上设置行车路线 默认起点是当前位置,终点是查询的地点 // NSDictionary* options =[[NSDictionary alloc]initWithObjectsAndKeys: // MKLaunchOptionsDirectionsModeDriving驾车路线,MKLaunchOptionsDirectionsModeKey设定路线模式, nil]; // // MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place]; // [mapItem openInMapsWithLaunchOptions:options]; //关闭键盘 [_txtQueryKey resignFirstResponder]; } }]; }