Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Adding custom annotations to a map

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 616
    Comment on it

    Annotations are little images(pins by default) that are to used to mark a point of interest in a map. But occasionally the programmer might need to use custom views or images instead of boring pins. This can be easily achieved by creating a image view and then adding the image view to the annotation. A single UIView, UIImageView and one UILabel can be used to display multiple annotations on the same map.

    The required code to perform the operation is as follows".

    -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)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:@"house.png"];
        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:@"car.png"];
    
        imgView.frame=CGRectMake(8,25,55,48);
        imgView.image=[UIImage imageNamed:@"plane.png"];
        lblName.frame=CGRectMake(5,79,60,10);
    
        [anView addSubview:bgImg];
        [anView addSubview:imgView];
        [newAnnotation addSubview:anView];
    
        newAnnotation.canShowCallout=YES;
        [newAnnotation setEnabled:YES];
    
        return newAnnotation;
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: