Adding PinPoint Annotation on map

suggest change

For annotating some point of interest on map, we use pin annotation. Now, start by creating annotation object first.

MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc] init];

Now provide coordinate to pointAnnotation,as

CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(23.054625,72.534562);
pointAnnotation.coordinate = coordinate;

Now, provide title and subtitle to annotation,

pointAnnotation.title = @"XYZ Point";
pointAnnotation.subtitle = @"Ahmedabad Area";

Now, add this annotation to map.

[self.mapView addAnnotation:pointAnnotation];

Yeaah.. Hurrah.. you have done the job. You can now see point annotation(red coloured pin) at given coordinate.

But now, what if you want to change color of the pin(3 available colors are - Purple,red and green). Then follow this step.

set mapview’s delegate to self,

self.mapView.delegate = self;

Add MKMapViewDelegate implementation. Now add following method then,

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
   // If it's the user location, just return nil, because it have user location's own annotation, if you want to change that, then use this object;
   if ([annotation isKindOfClass:[MKUserLocation class]])
       return nil;

   if ([annotation isKindOfClass:[MKPointAnnotation class]])
   {
       //Use dequed pin if available
       MKAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"PinAnnotationView"];
   
       if (!pinView)
       {
           // If not dequed, then create new.
           pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PinAnnotationView"];
           pinView.canShowCallout = YES;
           pinView.image = [UIImage imageNamed:@"abc.png"];
           pinView.calloutOffset = CGPointMake(0, 32);
       } else {
           pinView.annotation = annotation;
       }
       return pinView;
   }
   return nil;
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents