我有一个MKMapView,我正在添加一个MKPlacemark来表示用户刚刚选择的建筑物的位置。用户一次只能选择一个建筑物,我只想在他们选择新建筑时将地标移动到新建筑物。在他们选择的第一个建筑物上,它工作正常,并在地图上放置一个引脚。当我试着打电话 setCoordinate 在广场上更新标记的位置,当他们选择一个新的建筑,虽然,我得到 -[MKPlacemark setCoordinate:]: unrecognized selector sent to instance

在MyViewController中。h我有:

@property (nonatomic, strong)MKPlacemark *selectedBuildingPlacemark;

在MyViewController中。m

@synthesize selectedBuildingPlacemark;

...

if (self.selectedBuildingPlacemark == nil) {
        self.selectedBuildingPlacemark = [[MKPlacemark alloc] initWithCoordinate:myCoord addressDictionary:nil];
        [mapView addAnnotation:self.selectedBuildingPlacemark];
    }
    else {
        [self.selectedBuildingPlacemark setCoordinate:myCoord];
    }

我认为MKPlacemark符合MKAnnotation,因此应该实现 setCoordinate.有人可以告诉我我的方式的错误吗?

有帮助吗?

解决方案

的文件 MKAnnotation 说:

支持拖动的注解应该实现此方法来更新注解的位置。

所以方法 setCoordinate: 是可选的,仅由支持拖动的类实现。的文件 MKPlacemark 不引用该方法,因此未实现。

因此,您应该在每次选择新建筑时创建一个新实例。

其他提示

如果你不需要特别使用 MKPlacemark (它看起来不像,因为你正在为addressDictionary传递nil),你可以使用 MKPointAnnotation 类代替。

MKPointAnnotation 工具/工具 MKAnnotation 但增加了一个 setCoordinate 方法。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top