问题
I got a question about the MKAnnotations. I want to add to my mapView annotations to draw them with the typical Pin. How can I do it? I've implemented the coordinate getter because coordinate is readonly, and I gave value to the coordinates in that method. If I add the annotations like this: [self.mapView addAnnotations:self.annotations]; Isn't it enough? Because in my mapView don't appear the pins, and I don't know if a have to call some method, or I have to call a setter for the coordinate (but i thought that the own mapView call the getter of the Annotation class giving that coordinate to each Annotation). My mapView.annotations has annotations but they dont appear in the map.
Thanks
回答1:
All you need to do is
PinObject* pin = [[PinObject alloc] init];
[pin setCoordinate:CLLocationCoordinate2DMake(double latitude, double longitude)];
[map addAnnotation:pin];
In the PinObject you need to extends MKAnnotation and your .h file will look like:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface PinObject : NSObject <MKAnnotation>{
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate ;
@end
You also need to add the MapKit framework and the CoreLocation framework to your project
In your PinObject.m file you need to implement the method
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
coordinate = newCoordinate;
}
I hope I helped you.
来源:https://stackoverflow.com/questions/9821243/how-to-show-the-annotations-in-a-mapview