【问题标题】:How to show the annotations in a mapView如何在 mapView 中显示注释
【发布时间】:2012-03-22 19:39:24
【问题描述】:

我有一个关于 MKAnnotations 的问题。我想添加到我的 mapView 注释以使用典型的 Pin 绘制它们。我该怎么做?我已经实现了坐标获取器,因为坐标是只读的,并且我在该方法中为坐标赋值。如果我添加这样的注释: [self.mapView addAnnotations:self.annotations]; 还不够吗?因为在我的mapView中没有出现大头针,而且我不知道是否必须调用某个方法,或者我必须调用坐标的setter(但我认为自己的mapView调用Annotation类的getter将该坐标赋予每个注释)。我的 mapView.annotations 有注释,但它们没有出现在地图中。

谢谢

【问题讨论】:

    标签: objective-c google-maps mkannotationview


    【解决方案1】:

    你需要做的就是

    PinObject* pin = [[PinObject alloc] init];
    [pin setCoordinate:CLLocationCoordinate2DMake(double latitude, double longitude)];
    [map addAnnotation:pin];
    

    在 PinObject 中,您需要扩展 MKAnnotation,您的 .h 文件将如下所示:

    #import <Foundation/Foundation.h>
    #import <MapKit/MapKit.h>
    
    @interface PinObject : NSObject <MKAnnotation>{
    }
    
    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
    - (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate ;
    @end
    

    您还需要将 MapKit 框架和 CoreLocation 框架添加到您的项目中

    在您的 PinObject.m 文件中,您需要实现该方法

    - (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
        coordinate = newCoordinate;
    }
    

    希望对你有所帮助。

    【讨论】:

    • 完美!!有效!!谢谢。我的问题是我没有调用 setCoordinate 方法。我是一个初学者:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多