How to show the annotations in a mapView

我与影子孤独终老i 提交于 2019-12-25 08:29:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!