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:


MKMapView:
* Adding PinPoint Annotation on map

Table Of Contents
12 UIView
14 MKMapView
15 UIColor
26 UIImage
28 CALayer
30 NSDate
40 iBeacon
49 NSTimer
79 NSURL
87 AWS SDK
96 NSData
101 Segues
104 EventKit
105 NSBundle
106 SiriKit
111 StoreKit
117 3D Touch
119 Keychain
122 Block
141 AirDrop
144 UISlider
145 Carthage
146 HealthKit
151 plist
157 MVVM
164 UIPhoenix
166 Simulator
168 NSArray
169 OpenGL
175 Core Data
179 MyLayout
180 UIFont
189 Security
200 Codable