Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quickly adding single pin to MKMapView?

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).

like image 321
fuzzygoat Avatar asked Mar 27 '12 14:03

fuzzygoat


People also ask

How do you add annotations in Mkmapview?

Start by making your view controller the delegate of your map view, so that we can receive events. You should also make your view controller conform to MKMapViewDelegate in code. Second, you need to implement a viewFor method that converts your annotation into a view that can be displayed on the map.


2 Answers

Instead of using the -mapView:viewForAnnotation: method, just put the code for an MKPointAnnotation into your -viewDidLoad method. It won't animate the drop, but it is very easy.

// Place a single pin MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; [annotation setCoordinate:centerCoordinate]; [annotation setTitle:@"Title"]; //You can set the subtitle too [self.mapView addAnnotation:annotation]; 

Swift version:

let annotation = MKPointAnnotation() let centerCoordinate = CLLocationCoordinate2D(latitude: 41, longitude:29) annotation.coordinate = centerCoordinate annotation.title = "Title" mapView.addAnnotation(annotation) 
like image 73
nevan king Avatar answered Sep 29 '22 09:09

nevan king


You can set the point and also the region like this:

CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(lat, lon);  MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1); MKCoordinateRegion region = {coord, span};  MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; [annotation setCoordinate:coord];  [self.staticMapView setRegion:region]; [self.staticMapView addAnnotation:annotation]; 
like image 27
Hossam Ghareeb Avatar answered Sep 29 '22 11:09

Hossam Ghareeb