自定義一個(gè)需要的氣泡view:AliMapViewCustomCalloutView,繼承自UIView
新建一個(gè)繼承自MAAnnotationView的類:AliMapViewCustomAnnotationView,需要導(dǎo)入頭文件#import<MAMapKit/MAMapKit.h>
在.m文件中引用AliMapViewCustomCalloutView.h 并重寫選中方法
//重寫選中方法setSelected。選中時(shí)新建并添加calloutView,傳入數(shù)據(jù);非選中時(shí)刪除calloutView
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
? ? if(self.selected== selected)
? ? {
? ? ? ? return;
? ? }
? ? if(selected)
? ? {
? ? ? ? if(self.calloutView==nil)
? ? ? ? {
? ? ? ? ? ? self.calloutView = [[RRParkCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
? ? ? ? ? ? self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -CGRectGetHeight(self.calloutView.bounds) / 2.f +self.calloutOffset.y);
? ? ? ? }
? ? ? ? self.calloutView.titleLabel.text = self.annotation.title;
? ? ? ? self.calloutView.contentLabel.text = self.annotation.subtitle;
? ? ? ? [self addSubview:self.calloutView];
? ? ? ? if([self.standardintValue] == 0) {
//設(shè)置不同狀態(tài)下的選中大頭針圖片
?? ? ? ? ? ?self.image = [UIImage imageNamed:@"parkFreeClick"];
? ? ? ? }else{
? ? ? ? ? ? self.image = [UIImage imageNamed:@"parkChargeClick"];
? ? ? ? }
? ? }
? ? else
? ? {
? ? ? ? if([self.standardintValue] == 0) {
? ? ? ? ? ? self.image = [UIImage imageNamed:@"parkFree"];
? ? ? ? }else{
? ? ? ? ? ? self.image = [UIImage imageNamed:@"parkCharge"];
? ? ? ? }
? ? ? ? [self.calloutView removeFromSuperview];
? ? }
? ? [supersetSelected:selectedanimated:animated];
}
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event {
? ? UIView*view = [superhitTest:pointwithEvent:event];
? ? if(view ==nil) {
? ? ? ? CGPoint tempoint = [self.calloutView.naviButton convertPoint:point fromView:self];
? ? ? ? CGPoint dePoint = [self.calloutView.detailButton convertPoint:point fromView:self];
? ? ? ? if (CGRectContainsPoint(self.calloutView.naviButton.bounds, tempoint))
? ? ? ? {
? ? ? ? ? ? view =self.calloutView.naviButton;
//點(diǎn)擊導(dǎo)航按鈕的響應(yīng)
? ? ? ? ? ? [[NSNotificationCenter defaultCenter] postNotificationName:@"naviPark" object:self.annotation.title];
? ? ? ? ? ? [self.calloutView removeFromSuperview];
? ? ? ? }else if(CGRectContainsPoint(self.calloutView.detailButton.bounds, dePoint)){
? ? ? ? ? ? view =self.calloutView.detailButton;
//點(diǎn)擊查看詳情按鈕的響應(yīng)
? ? ? ? ? ? [[NSNotificationCenter defaultCenter] postNotificationName:@"detailPark" object:self.annotation.title];
? ? ? ? ? ? [self.calloutView removeFromSuperview];
? ? ? ? }else{
? ? ? ? ? ? [self.calloutView removeFromSuperview];
? ? ? ? }
? ? }
? ? returnview;
}
在controller中調(diào)用
static NSString *reuseIndetifier = @"annotationReuseIndetifier";
? ? ? ? AliMapViewCustomAnnotationView *annotationView = (AliMapViewCustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
? ? ? ?if(annotationView ==nil)
? ? ? ? {
? ? ? ? ? ? annotationView = [[AliMapViewCustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
? ? ? ? }
