如何使用新坐标更新 MKAnnotation 位置?

发布于 2024-09-02 07:32:44 字数 454 浏览 11 评论 0原文

我在尝试使用不同坐标更新注释位置时遇到问题。有什么方法可以更改位置属性而无需创建另一个注释?我尝试过下面的代码,但没有成功。我试图获取的注释没有更新其位置。请帮忙!

CLLocationCoordinate2D location;
location.latitude = -36.560976;
location.longitude = -59.455807; 

for (id annotation in self.mapView.annotations) {

    if ([annotation isKindOfClass:[MyAnnotation class]]) 
    {
            [annotation setCoords:location]; //setCoords is defined in MyAnnotation class
            }
    }

谢谢大家!

i'm having problems trying to update an annotation location with different coordinates. Is there any way I can change de location property without having to create another annotation? I've tried the code below with no luck. The annotation i'm trying to get is not updating its location. Please help!

CLLocationCoordinate2D location;
location.latitude = -36.560976;
location.longitude = -59.455807; 

for (id annotation in self.mapView.annotations) {

    if ([annotation isKindOfClass:[MyAnnotation class]]) 
    {
            [annotation setCoords:location]; //setCoords is defined in MyAnnotation class
            }
    }

Thank you all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

海螺姑娘 2024-09-09 07:32:44

扩展MKPointAnnotation,它具有自动将地图上的注释视图更新到新坐标的能力。

@interface MyAnnotation : MKPointAnnotation

Extend MKPointAnnotation, it has the capability to automatically update the annotation view on the map to the new coordinates.

@interface MyAnnotation : MKPointAnnotation
安稳善良 2024-09-09 07:32:44

您需要删除注释并在更改其坐标后重新添加它,或者用新注释替换它。 MapKit 不假设注释可能是可变的。

You need to remove the annotation and add it again after changing its coordinates, or replace it with a new one. MapKit does not assume annotations might be mutable.

翻了热茶 2024-09-09 07:32:44

您不必先删除注释然后重新添加它才能更新注释在地图上的位置。

您添加注释的 MapView 应该为您处理位置更改。在没有看到更多代码的情况下,我认为您遇到的问题可能是您的注释使用 location 变量而不是 cooperative 变量。

MKAnnotation 协议要求您在注释类中实现 CLLocationCooperative2D 坐标 实例变量。它还建议您综合坐标属性。

尝试向注释类 .h 文件中添加 CLLocationCooperative2D 坐标 实例变量及其匹配属性 @property(分配,非原子)CLLocationCooperative2D 坐标。然后在注释类 .m 文件中使用 @synthesize

如果这不能解决您的问题,您可能需要为您的问题添加更多详细信息。

You should not have to remove your annotation and then re-add it in order to update the annotation's location on the map.

The MapView that you have added your annotation to should take care of location changes for you. Without seeing more code, I think the problem you are having might be that your annotation uses a location variable rather than a coordinate variable.

The MKAnnotation protocol requires that you implement a CLLocationCoordinate2D coordinate instance variable in your annotation class. It also recommends that you synthesize a coordinate property.

Try adding the CLLocationCoordinate2D coordinate instance variable and its matching property @property (assign, nonatomic) CLLocationCoordinate2D coordinate to your annotation class .h file. Then use a @synthesize in your annotation class .m file.

If this doesn't solve your problem you might have to add more detail to your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文