Adding PinPoint Annotation on map
suggest changeFor 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 a coordinate to pointAnnotation:
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.
To draw a custom image instead of the default pin, set the map view's delegate and implement mapView:viewForAnnotation:. The older purple, red, and green pin tints belong on MKPinAnnotationView (pinTintColor), not on this image-based view.
Set the map view'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, return nil so MapKit keeps its own annotation. Return the user-location object only when you want to replace it.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
// Use a dequeued pin if one is available
MKAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"PinAnnotationView"];
if (!pinView)
{
// If nothing was dequeued, create a new view.
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;
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents