“地圖上自定義的標註點和覆蓋物我們統稱爲地圖覆蓋物。您可以通過定製BMKAnnotation和BMKOverlay來添加對應的標註點和覆蓋物。地圖覆蓋物的設計遵循數據與View分離的原則,BMKAnnotation和BMKOverlay系列的類主要用來存放覆蓋物相關的數據,BMKAnnotaionView和BMKOverlayView系列類爲覆蓋物對應的View。”
實現BMKMapViewDelegate的_mapView:viewForAnnotation:函數,並在viewDidAppear添加標註數據對象。
【注意】我曾在viewDidLoad中添加標註數據,導致代理方法_mapView:viewForAnnotation:不會被調用,不顯示標註視圖。修改爲在viewWillAppear方法中添加時就好了,原因是什麼。。
[_mapView
addAnnotation:annotation]; // 添加標註點會調用代理方法_mapView:viewForAnnotation:,該代理方法用於爲標註點生成相應的標註視圖。
- (void) viewDidAppear:(BOOL)animated {
// 添加一個PointAnnotation
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor;
coor.latitude = 39.915;
coor.longitude = 116.404;
annotation.coordinate = coor;
annotation.title = @"這裏是北京";
[_mapView addAnnotation:annotation];
}
// Override
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
newAnnotationView.animatesDrop = YES;// 設置該標註點動畫顯示
return newAnnotationView;
}
return nil;
}