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
    • 575
    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];
            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];
        UILabel *lblName=[[UILabel alloc] init];
        lblName.font=[UIFont systemFontOfSize:12];
        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.image=[UIImage imageNamed:@"plane.png"];
        [anView addSubview:bgImg];
        [anView addSubview:imgView];
        [newAnnotation addSubview:anView];
        [newAnnotation setEnabled:YES];
        return newAnnotation;

 0 Comment(s)

Sign In

Sign up using

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: