I have a GPS coordinate (latitude, longitude) and I quickly want to place a single pin on a MKMapView showing that position. Everything works just fine, but as I only need a single pin with no callout is there a quicker way to do this or is what I have below what needs to be done?
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation {
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"DETAILPIN_ID"];
[pinView setAnimatesDrop:YES];
[pinView setCanShowCallout:NO];
return pinView;
}
NB: I don't need to check for reusable annotation views as I am only using the pin to show a position in a detail view (which is destroyed and recreated the next time a detail view is requested).
Since you want to animate the drop, you will need to implement
viewForAnnotation
as you've done because that property isNO
by default.If you don't need to animate the drop, you could eliminate the
viewForAnnotation
method implementation completely and to disable the callout, set the annotation'stitle
to nil or blank when adding the annotation instead.If you do need the pin drop animation and you're also showing the user's location (blue dot), you'll also need to check for
MKUserLocation
inviewForAnnotation
and returnnil
.Otherwise, you could remove the whole
viewForAnnotation
method and the default red pin will appear without animation, the callout will show iftitle
is not blank, and the user location will show as a blue dot.Swift version of add pin in MKMapView:
Swift version of focusing a region or pin
To learn more details visit "Load MKMapView using Swift".
Simply you can add it like:
It's look like:
Instead of using the
-mapView:viewForAnnotation:
method, just put the code for anMKPointAnnotation
into your-viewDidLoad
method. It won't animate the drop, but it is very easy.Swift version:
The easiest and simplest method to drop a pin and displaying Current Location in Map View is to simply add this code in your viewDidLoad method. (Assuming that user's current location is already fetched by you).
You can set the point and also the region like this: