iphone - 快速将单个引脚添加到 MKMapView?

我有一个 GPS 坐标(纬度、经度),我很快想在 MKMapView 上放置一个大头针来显示该位置。一切正常,但由于我只需要一个没有标注的大头针,有没有更快的方法来做到这一点,或者我有什么需要做的?

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation {
    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"DETAILPIN_ID"];
    [pinView setAnimatesDrop:YES];
    [pinView setCanShowCallout:NO];
    return pinView;
}

注意:我不需要检查可重复使用的注释 View ,因为我只使用图钉来显示详细 View 中的位置(在下次请求详细 View 时将其销毁并重新创建)。

最佳答案

不要使用 -mapView:viewForAnnotation: 方法,只需将 MKPointAnnotation 的代码放入您的 -viewDidLoad 方法中。它不会为水滴设置动画,但非常简单。

// Place a single pin
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:centerCoordinate];
[annotation setTitle:@"Title"]; //You can set the subtitle too
[self.mapView addAnnotation:annotation];

Swift 版本:

let annotation = MKPointAnnotation()
let centerCoordinate = CLLocationCoordinate2D(latitude: 41, longitude:29)
annotation.coordinate = centerCoordinate
annotation.title = "Title"
mapView.addAnnotation(annotation)

https://stackoverflow.com/questions/9891926/

相关文章:

iphone - NSNumber 和 NSInteger 有什么区别?

objective-c - 将 Xcode 组创建为文件系统文件夹的工作流程

ios - 如何使用图像和标签制作自定义 UIBarButtonItem?

objective-c - 具有多个目标的 Objective C 到 Swift 头文件

objective-c - 将参数传递给由 NSTimer 调用的方法

ios - '无效更新 : invalid number of rows in section 0

ios - 按字典中键的值对字典的 NSArray 进行排序

c++ - 我可以在同一个 Xcode 项目中拥有 Swift、Objective-C、C 和 C+

objective-c - 将自定义 subview (在 xib 中创建)添加到 View Con

ios - MKMapView:自定义 View 代替 Annotation Pin