【问题标题】:iPhone MapKit - update annotations coordinates and mapiPhone MapKit - 更新注释坐标和地图
【发布时间】:2009-12-09 13:05:26
【问题描述】:

我使用本教程将 MapKit 集成到我的应用程序中: http://iphonebcit.wordpress.com/iphone-map-kit-tutorial/

CLLocationCoordinate2D coordinate;
coordinate.latitude = 49.2802;
coordinate.longitude = -123.1182;

NSUInteger count = 1;
for(int i = 0; i < 10; i++) {
    CGFloat latDelta = rand()*.035/RAND_MAX - .02;
    CGFloat longDelta = rand()*.03/RAND_MAX - .015;

    CLLocationCoordinate2D newCoord = {coordinate.latitude+latDelta, coordinate.longitude+longDelta};
    MapDemoAnnotation* annotation = [[MapDemoAnnotation alloc] initWithCoordinate:newCoord andID:count++];
    [mapView addAnnotation:annotation];
    [annotation release];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapViewLocal viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapViewLocal dequeueReusableAnnotationViewWithIdentifier:@"Pin"];
if(pinView == nil) {
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
    pinView.pinColor = MKPinAnnotationColorPurple;
    pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinView.animatesDrop = YES;
    pinView.canShowCallout = YES;
} else {
    pinView.annotation = annotation;
}
return pinView;

}

所以图钉会随机设置在地图上。在我的应用程序中,坐标会改变。如何更改注释的坐标,以便它们在地图上更新?

有人知道吗?

【问题讨论】:

    标签: iphone objective-c mapkit


    【解决方案1】:

    在 iPhone SDK 3.x 中,您必须删除引脚注释并重新设置。如果您的地图有很多注释,那就不是很好了。

    我试图让它变得更好,所以我只显示/更新我在屏幕上的 pin 注释。因此,如果用户放大到纽约,则在旧金山或其他用户看不到的地方不会有 pin 注释。所以性能会好很多。

    也许在未来这是可能的。我希望如此:-)

    【讨论】:

      【解决方案2】:

      该教程仅用于获取要显示的地图视图,仅此而已。你将需要一个更大的教程。我发现这个很有用:

      http://blog.objectgraph.com/index.php/2009/04/02/iphone-sdk-30-playing-with-map-kit/

      您可能需要做的是遍历地图上的所有注释,删除它们,然后遍历您的数据数组,再次添加注释。你可以更聪明一点,循环遍历数据数组,检查地图上是否已经有一个具有相同纬度/经度的图钉,但这种方式会变得更加复杂。

      【讨论】:

      • 非常感谢。我实现的注释协议有一个名为 locationId 的附加实例变量。这个 id 是唯一的,我知道应该用新坐标更新哪个 locationId。我怎样才能轻松访问它?我可以删除注释:“使用 locationId=3999 删除注释并使用新坐标添加它吗?” - 更好:“使用 locationId=3999 的注释设置新坐标并在 pin 时更新位置”。
      • 啊,我忘了。我已经看过这个教程,但这对我没有帮助,因为它没有任何用处可以解决我的问题。
      【解决方案3】:

      由于我现在正在做注释,所以我只是做了一个快速测试。您确实会收到编译器警告,因此它可能不受支持。但它有效。

      制作一个自定义的 MKAnnotation 类,以便您可以将坐标属性设置为可写:

      @property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
      

      然后在您想要的任何事件或间隔,使用以下方式更改坐标:

          CLLocation *loc=[[CLLocation alloc] initWithLatitude:55.0 longitude:17.0];
      annotation.coordinate=loc.coordinate;
      [loc release];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-24
        • 1970-01-01
        相关资源
        最近更新 更多