I am interacting with the Google Places API and I am trying to get the list of coordinates of the restaurants, and display them in the mapView as pins. I also want to create a callout for it.
What I have done so far is: Get the longitude and latitude of the locations (along with name and address), store them in an Array, and I do
[self.nearmapView addAnnotations:self.AnnoArray];
The MapView display the pins as Red although in my below method I had it as Green.
- (MKAnnotationView *)mapView:(MKMapView *)theMapView
viewForAnnotation:(id <MKAnnotation>)annotation
{
customPinView.pinColor = MKPinAnnotationColorGreen;
}
It was even crazier when I commented the above method, the mapView can still generate the annotation pins (so the above method wasn’t called at all).
How do I fix this?
Does anyone know how to use storyboard to make annotation connection with segue?
If you don’t implement
mapView:viewForAnnotation:, or if for some reason it’s not getting called, you get the default red pin view.You can make sure your
mapView:viewForAnnotation:is getting called by sticking anNSLogat the top or setting a breakpoint. If you find it’s not, you may have failed to hook up your delegate. There’s more than one way to do this, but often it’s done like so:MKMapViewDelegateprotocol:@interface MyViewController : UIViewController <MKMapViewDelegate>self.mapView.delegate = selfinviewDidLoad, presuming you have a propertymapViewpointing to the map view).mapView:viewForAnnotation:.If you do step 3 without steps 1 and 2, your
mapView:viewForAnnotation:will never be called.If the
mapView:viewForAnnotation:you’ve posted is the entire implementation and not an excerpt, I can see why it’s not working — you’re not returning the instance ofMKAnnotationViewthat you’re setting the color on. You need to return an actual view, and it needs to be a new instance for each distinct annotation.As for storyboarding — obviously your annotations aren’t visible in IB, so you can’t drag segues from them. Instead, drag a segue from the view controller itself (the one managing the map view), and give it an identifier. Then, when the annotation’s button is tapped (which invokes the delegate method
mapView:annotationView:calloutAccessoryControlTapped:), your view controller can call[self performSegueWithIdentifier:@"yourIdentifier"]to make it happen. (You don’t get access to the segue’s destination view controller at that time, so you can’t configure it based on which annotation was tapped. But you can remember which annotation it was and then use it inprepareForSegue:sender:.)