關于設置代理 直接在此復制官方的設置:
-(void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此處記得不用的時候需要置nil,否則影響內存的釋放
}
-(void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用時,置nil
}
我所遇到的坑是,自定義大頭針時的坑。因為設置代理是在viewWillAppear中,而我直接就在viewdidload中直接添加大頭針,所以很坑的事情來了,怎么都不顯示自定義的大頭針圖片。
-
(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;// 設置該標注點動畫顯示 newAnnotationView.annotation = annotation; newAnnotationView.paopaoView = nil; newAnnotationView.image = [UIImage imageNamed:@"btn_weizhi_selected"]; UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAciton:)]; //設置點按的次數 tap.numberOfTapsRequired = 1; //設置點按的手指的數 tap.numberOfTouchesRequired = 1; // 2.為控件添加手勢 [newAnnotationView addGestureRecognizer:tap]; return newAnnotationView;
}
return nil;
}
上述是mapview的一個代理方法,如果你直接在viewdidload中添加大頭針是無法改變官方的大頭針圖片的,你只需要換個地方,例如網絡請求中,刷新位置按鈕然后觸發添加大頭針的方法即可,不知道你理解我的意思沒。
PS:記錄自己開發中遇到的坑,不喜勿噴。。。??