我是swift语言的新手,还没有使用mapkit完成应用程序。但是我已经设置了地图和区域,但我已经挂断了如何允许用户添加引脚。 让我澄清一下,我甚至不知道从哪里开始,我现在所拥有的(针脚)是我的变量,但我甚至不确定这是否正确。任何帮助将非常感激!! 我有什么......
var MyPins:MKPinAnnotatoinView!
...
override func viewDidLoad() {
super.viewDidLoad()
Mapview代码
..... ..... }
答案 0 :(得分:38)
你的pin变量是正确的。现在您只需要将此注释添加到MKMapView
。
您还可以为MKAnnotation
创建自定义类,以便为地图视图添加自定义注释。
MapExampleiOS8 =>的测试版演示其中支持 Swift 2.1
按照以下步骤操作:
<强> 1。将MapKit.framework
添加到项目中。
<强> 2。创建地图视图控件的Storyboard变量IBOutlet
或在视图控制器中创建它。设置此变量的委托以覆盖它的委托方法:
将代理签名添加到视图控制器界面:
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.isKind(of: CustomAnnotation.self)) {
let customAnnotation = annotation as? CustomAnnotation
mapView.translatesAutoresizingMaskIntoConstraints = false
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "CustomAnnotation") as MKAnnotationView!
if (annotationView == nil) {
annotationView = customAnnotation?.annotationView()
} else {
annotationView?.annotation = annotation;
}
self.addBounceAnimationToView(annotationView)
return annotationView
} else {
return nil
}
}
<强> 4。将MKPointAnnotation
添加到地图视图。
您可以在地图视图中将pin添加到位置。为简单起见,将代码添加到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)
}
答案 1 :(得分:0)
您需要为用户需要添加引脚的时间和位置调用方法。如果你想要它添加一个用户在地图上点击并保持的引脚,你需要调用一个gestureRecognizer,但是如果你想通过一个按钮来做,你显然只是在一个动作中调用它。无论哪种方式,都会详细讨论添加引脚的文档Here