How to add annotation in Mapview with image and label in IPhone?

后端 未结 2 1204
谎友^
谎友^ 2021-02-02 04:21

\"image1\" \"image

I am using MKMapview in my i

2条回答
  •  醉梦人生
    2021-02-02 05:01

    You can use custom view for each annotation with one UIView, one UIImageView and one Label to display different value. This will be done within the method - (MKAnnotationView *)mapView:(MKMapView *)newMapView viewForAnnotation:(id )newAnnotation. You have to take UIView double in size with transparent color with respect to UIImageView to make more precise view on zoom in and zoom out in mapview.

    Code-----

    -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation
    {
        static NSString * const kPinAnnotationIdentifier = @"PinIdentifier";
        if(annotation == self._mapView.userLocation)
        {
            return nil;
        }
    
        MyAnnotation *myAnnotation  = (MyAnnotation *)annotation;
        MKAnnotationView *newAnnotation = (MKAnnotationView*)[self._mapView dequeueReusableAnnotationViewWithIdentifier:kPinAnnotationIdentifier];
    
        if(!newAnnotation){
            newAnnotation = [[MKAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:@"userloc"];
        }
    
        NSDictionary *dict=[alertInfoArray objectAtIndex:myAnnotation.ann_tag];
        UIView *anView=[[UIView alloc] init];
        anView.backgroundColor=[UIColor clearColor];
    
        UIImageView *bgImg=[[UIImageView alloc] init];
        bgImg.image=[UIImage imageNamed:@""];
        bgImg.backgroundColor=[UIColor clearColor];
    
        UIImageView *imgView=[[UIImageView alloc] init];
        imgView.tag=myAnnotation.ann_tag;
    
        UILabel *lblName=[[UILabel alloc] init];
        lblName.font=[UIFont systemFontOfSize:12];
        lblName.textAlignment=UITextAlignmentCenter;
        lblName.textColor=[UIColor whiteColor];
        lblName.backgroundColor=[UIColor clearColor];
        lblName.text=TEXT YOU WANT ;
    
        newAnnotation.frame=CGRectMake(0, 0, 70, 212);
        anView.frame=CGRectMake(0, 0, 70, 212);
        bgImg.frame=CGRectMake(0, 0, 70, 106);
        bgImg.image=[UIImage imageNamed:@"thumb-needhelp.png"];
    
        imgView.frame=CGRectMake(8,25,55,48);
        imgView.image=[UIImage imageNamed:@"girl-default.png"];
        lblName.frame=CGRectMake(5,79,60,10);
    
    
    
        [anView addSubview:bgImg];
        [bgImg release];
        [anView addSubview:imgView];
        [imgView release];
        [newAnnotation addSubview:anView];
        [anView release];
    
        newAnnotation.canShowCallout=YES;
        [newAnnotation setEnabled:YES];
    
        return newAnnotation;
    }
    

    Hope this helps.

提交回复
热议问题