如何使用Xcode 6(Swift)添加引脚(注释)

我对新语言很陌生,还没有用mapkit做过应用。 但我有地图和地区设置,但我挂在如何让用户添加引脚。 让我澄清,我甚至不知道从哪里开始,现在我所有的(针脚)是我的variables,但我甚至不确定这是否正确。 任何帮助将非常感激!! 我拥有的…

var MyPins:MKPinAnnotatoinView!

……

override func viewDidLoad() { super.viewDidLoad() 

Mapview代码

….. …..}

你的引脚variables是正确的。 现在你只需要把这个注解添加到MKMapView

您还可以为MKAnnotation创build自定义类,以将自定义注释添加到地图视图。

MapExampleiOS8的testing版演示=>其中支持Swift 2.1

遵循以下步骤:

1.将MapKit.framework添加到项目。

在这里输入图像说明

2.创build地图视图控件的StoryboardvariablesIBOutlet或在视图控制器中创build它。 为此variables设置委托来覆盖它的委托方法:

添加委托签名到视图控制器界面:

 class ViewController: UIViewController, MKMapViewDelegate { override func viewDidLoad() { super.viewDidLoad() // Set map view delegate with controller self.mapView.delegate = self } } 

3.重写它的委托方法:

这里我们需要重写mapView(_:viewForAnnotation:)方法来在地图上显示注释引脚。

 func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { if (annotation is MKUserLocation) { return nil } if (annotation.isKindOfClass(CustomAnnotation)) { let customAnnotation = annotation as? CustomAnnotation mapView.translatesAutoresizingMaskIntoConstraints = false var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("CustomAnnotation") as MKAnnotationView! if (annotationView == nil) { annotationView = customAnnotation?.annotationView() } else { annotationView.annotation = annotation; } self.addBounceAnimationToView(annotationView) return annotationView } else { return nil } } 

4.将MKPointAnnotation添加到地图视图。

您可以将地图添加到地图视图中的位置。 为了简单,将代码添加到viewDidLoad()方法。

 override func viewDidLoad() { super.viewDidLoad() // Set map view delegate with controller self.mapView.delegate = self let newYorkLocation = CLLocationCoordinate2DMake(40.730872, -74.003066) // Drop a pin let dropPin = MKPointAnnotation() dropPin.coordinate = newYorkLocation dropPin.title = "New York City" mapView.addAnnotation(dropPin) } 

您将需要调用一个方法来确定用户需要添加引脚的时间和地点。 如果你想要在地图上添加一个别针,你需要调用一个gestureRecognizer,但是如果你想通过一个button来做,你显然会在一个动作中调用它。 无论哪种方式,添加引脚的文档都在这里讨论