自定义注释视图需要双击以在调用视图显示时打开详细注释视图

时间:2015-02-08 06:52:27

标签: ios iphone mapkit mkannotationview mkmapviewdelegate

**这里首先,我们创建自定义注释视图,其中包含图像,按钮和其他信息。我们将地图作为图像标记。我们也隐藏了呼出视图。

第二个条件仅包含地图标记图像并显示调出视图。 现在,问题是如果打开呼出视图然后我们点击自定义视图它需要双击。

我们检查了第一次点击调用地图取消选择方法然后我们获得按钮点击事件。

如果我们只点击不同的呼出视图或不同的自定义图像标记视图,那么它正在单击。在这种情况下,如何在单击中处理这两种情况?**

//Add image marker
  -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
            {
                if([annotation isKindOfClass:[MKUserLocation class]])
                    return nil;

                int ID = [((NewClass *)annotation).uniqueID intValue];

                static NSString *identifier = @"myAnnotation";

                annotationView = (CustomAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

               if (!annotationView)
                {
                    annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
                    annotationView.image = [UIImage imageNamed:@"profilemarker.png"];
                    annotationView.canShowCallout=NO;
                }
                else {
                    annotationView.annotation = annotation;
                }

                    if(array_Profiles[ID][@"id"])
                    {
                        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
                        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

                        NSDate *startDate = [dateFormatter dateFromString:array_Profiles[ID][@"start_time"]];
                        NSDate *endDate = [dateFormatter dateFromString:array_Profiles[ID][@"end_time"]];

                         annotationView.image = nil;

        **//Here Add Custom Annotation view. View contains add two Image view and Button. Here we hide annotationView.canShowCallout=NO** 

                        LiveProfileMarker *liveProMarkerView = (LiveProfileMarker *)[[[NSBundle mainBundle] loadNibNamed:@"LiveProfileMarker" owner:self options:nil] objectAtIndex:0];
                        liveProMarkerView.delegate = self;
                        CGRect liveViewFrame = liveProMarkerView.frame;
                        liveViewFrame.origin = CGPointMake(-liveViewFrame.size.width/2 + 15, -liveViewFrame.size.height);
                        liveProMarkerView.backgroundColor=[UIColor clearColor];
                        liveProMarkerView.frame = liveViewFrame;

                        if(([[NSDate date] compare: startDate] ==  NSOrderedDescending) && ([[NSDate date] compare: endDate] == NSOrderedAscending))
                            //Set Profile marker for live
                                [liveProMarkerView.img_banner setImage:[UIImage imageNamed:@"live_trans_banner.png"]];
                        else
                                [liveProMarkerView.img_banner setImage:nil];

                        [liveProMarkerView.img_ProfileIV setFrame:CGRectMake(5, 5, liveViewFrame.size.width-10, liveViewFrame.size.height-15)];
                        [liveProMarkerView.img_ProfileIV setImageWithURL:[NSURL URLWithString:array_Profiles[ID][@"logo"]] placeholderImage:nil];
                        liveProMarkerView.profile_Btn.tag = ID;
                        [annotationView addSubview:liveProMarkerView];
                        liveProMarkerView.annotationVI = annotationView;

                        annotationView.canShowCallout=NO;

                    }
                    else
                    {
                        annotationView.image = [UIImage imageNamed:@"profilemarker3.png"];

                        if([isFlyer intValue] == 0)
                        {
                            UIButton * btn_AssessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                            [btn_AssessoryView setTag:ID];
                            [btn_AssessoryView addTarget:self action:@selector(PinAnnoation:) forControlEvents:UIControlEventTouchUpInside];

                            [annotationView setRightCalloutAccessoryView:btn_AssessoryView];

                            //Changes Make Here
                            UIImageView * imageView = [[UIImageView alloc] init];
                           // AsyncImageView * imageView = [[AsyncImageView alloc] init];
                            [imageView setContentMode:UIViewContentModeScaleAspectFit];

                            if([array_Profiles[ID][@"logo"] length] == 0)
                            {
                                [imageView setFrame:CGRectMake(0, 0, 41, 30)];
                                [imageView setImage:[UIImage imageNamed:@"fly.png"]];
                            }
                            else
                            {
                                [imageView setFrame:CGRectMake(0, 0, 30, 30)];
                               // [imageView setImageWithURL:[NSURL URLWithString:array_Profiles[ID][@"logo"]] usingActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
                               // [imageView setImageURL:[NSURL URLWithString:array_Profiles[ID][@"logo"]]];
                               // imageView.showActivityIndicator = YES;

                                [imageView setImageWithURL:[NSURL URLWithString:array_Profiles[ID][@"logo"]] placeholderImage:nil];
                            }

                            annotationView.leftCalloutAccessoryView = imageView;

                        }

      //Add here image in annotation view and show Call Out
                        annotationView.canShowCallout=YES;
                    }

                return annotationView;
            }

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)viewmap
    {

        for (UIView *subView in calloutView.subviews){

            if ([subView isKindOfClass:[UIView class]]) {
                [subView removeFromSuperview];
            }
        }

    }



    //De Select View
    - (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
    {

        for (UIView *subView in calloutView.subviews){

            if ([subView isKindOfClass:[UIView class]]) {
                [subView removeFromSuperview];
            }
        }

    }


    //Below delegate method for Live Profile view Button Action

    -(void)ProfileButtonAction:(NSString *)markerID SelectedAnnotationView:(MKAnnotationView *)annotationObj profileMarkerFrame:(CGRect)frame{
       // NSLog(@"markerID : %@",markerID);

        for (UIView *subView in calloutView.subviews){

            if ([subView isKindOfClass:[UIView class]]) {
                [subView removeFromSuperview];
            }
        }

        NSMutableArray *annotAry = [[NSMutableArray alloc] init];
        [annotAry addObject:annotationObj];


        MKAnnotationView *viewmap = annotationObj;

        if(![viewmap.annotation isKindOfClass:[MKUserLocation class]])
        {
            NewClass *ann=(NewClass *)viewmap.annotation;

            [_mapView setCenterCoordinate:ann.coordinate animated:YES];

            int ID = [markerID intValue];

            if(array_Profiles[ID][@"id"]){

                calloutView = (CalloutView *)[[[NSBundle mainBundle] loadNibNamed:@"CalloutView" owner:self options:nil] objectAtIndex:0];

                calloutView.delegate=self;
                calloutView.backgroundColor=[UIColor clearColor];
                CGRect calloutViewFrame = calloutView.frame;
                calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, (frame.origin.y-calloutViewFrame.size.height));
                calloutView.frame =calloutViewFrame;

                calloutView.lbl_title.text=array_Profiles[ID][@"name"];
                calloutView.lbl_description.text=array_Profiles[ID][@"offer1"];
                calloutView.btn_click.tag=ID;

                NSArray *startArr=[api_CommonMethods tocanizeTheString:array_Profiles[ID][@"start_time"] tocan:@" "];
                NSArray *endArr=[api_CommonMethods tocanizeTheString:array_Profiles[ID][@"end_time"] tocan:@" "];

                NSString *startT=startArr[1];
                startT=[startT substringToIndex:startT.length-3];
                NSString *endT=endArr[1];
                endT=[endT substringToIndex:endT.length-3];
                NSString *timings=[NSString stringWithFormat:@"%@-%@",startT,endT];

                calloutView.lbl_timings.text=timings;

                [viewmap addSubview:calloutView];
            }

        }

    }

0 个答案:

没有答案