ios 如何使用 Xcode 6 (Swift) 添加 Pin 图(注释)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27349612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Do I add Pins (Annotations) with Xcode 6 (Swift)
提问by Kampai
I'm new to the swift language, and haven't done an application with mapkit yet. But I have the map and regions set, but I'm hung up on how to allow users to add pins. Let me clarify, I have no idea of even where to start, All I have at the moment (for the pins) is my variable, but I'm not even sure if that's correct. Any help would be much appreciated!! What I have...
我是 swift 语言的新手,还没有用 mapkit 做过应用程序。但是我已经设置了地图和区域,但是我对如何允许用户添加图钉犹豫不决。让我澄清一下,我什至不知道从哪里开始,我目前(对于引脚)所拥有的只是我的变量,但我什至不确定这是否正确。任何帮助将非常感激!!我拥有的...
var MyPins: MKPinAnnotatoinView!
var MyPins: MKPinAnnotatoinView!
......
......
override func viewDidLoad() {
super.viewDidLoad()
Mapview code
地图视图代码
..... ..... }
..... ..... }
回答by Kampai
Your pin variable is correct. Now you just need to add this annotation to MKMapView
.
您的 pin 变量是正确的。现在您只需要将此注释添加到MKMapView
.
You can also create a custom class for MKAnnotation
to add custom annotation to map view.
您还可以创建自定义类MKAnnotation
以将自定义注释添加到地图视图。
A beta demo for MapExampleiOS8=> Which supports Swift 2.1
MapExampleiOS8的测试版演示=> 支持Swift 2.1
Follow steps below:
请按照以下步骤操作:
1. Add MapKit.framework
to project.
1. 添加MapKit.framework
到项目。
2. Create Storyboard variable IBOutlet
of map view control or create it in view controller. Set delegate for this variable to override it's delegate methods:
2. 创建IBOutlet
地图视图控件的Storyboard 变量或在视图控制器中创建它。为这个变量设置委托以覆盖它的委托方法:
Add delegate signature to view controller interface:
将委托签名添加到视图控制器界面:
class ViewController: UIViewController, MKMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Set map view delegate with controller
self.mapView.delegate = self
}
}
3. Override its delegate methods:
3. 覆盖其委托方法:
Here we need to override mapView(_:viewForAnnotation:)
method to show annotation pins on map.
在这里,我们需要覆盖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. Add MKPointAnnotation
to map view.
4. 添加MKPointAnnotation
到地图视图。
You can add pin to location in map view. For simplicity add code to viewDidLoad()
method.
您可以在地图视图中将图钉添加到位置。为简单起见,向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)
}
回答by Nicholas
You will need to call a method for when and where the user needs to add the pin. If you want it to add a pin where the user taps and holds on the map, you will need to call a gestureRecognizer, but if you want to do it via a button you will obviously just call that in an action. Either way the documentation for adding pins is throughly discussed Here
您将需要为用户需要在何时何地添加 pin 调用一个方法。如果您希望它在用户在地图上点击并按住的位置添加一个图钉,您将需要调用gestureRecognizer,但如果您想通过按钮来完成,您显然只需在一个动作中调用它。无论哪种方式,添加引脚的文档都在这里进行了彻底讨论