如何使用Xcode 6(Swift)添加引脚(注释) [英] How Do I add Pins (Annotations) with Xcode 6 (Swift)

查看:115
本文介绍了如何使用Xcode 6(Swift)添加引脚(注释)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是swift语言的新手,还没有用mapkit做过应用程序。但是我已经设置了地图和区域,但是我已经挂断了如何允许用户添加引脚。
让我澄清,我甚至不知道从哪里开始,我现在所有的(针脚)是我的变量,但我甚至不确定这是否正确。任何帮助将非常感激!!
我有什么......

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...

var MyPins:MKPinAnnotatoinView!

var MyPins: MKPinAnnotatoinView!

..... 。

override func viewDidLoad() {
    super.viewDidLoad()

Mapview代码

.....
.....
}

..... ..... }

推荐答案

你的pin变量是正确的。现在您只需要将此注释添加到 MKMapView

Your pin variable is correct. Now you just need to add this annotation to MKMapView.

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

You can also create a custom class for MKAnnotation to add custom annotation to map view.

MapExampleiOS8 的测试版 =>支持 Swift 2.1

A beta demo for MapExampleiOS8 => Which supports Swift 2.1

按照以下步骤操作:

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

1. Add MapKit.framework to project.

2。创建地图视图控件的Storyboard变量 IBOutlet 或在视图控制器中创建它。设置此变量的委托以覆盖它的委托方法:

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:

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

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。覆盖其委托方法:

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

Here we need to override mapView(_:viewForAnnotation:) method to show annotation pins on map.

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 添加到地图视图。

4. Add MKPointAnnotation to map view.

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

You can add pin to location in map view. For simplicity add code to viewDidLoad() method.

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)
}

这篇关于如何使用Xcode 6(Swift)添加引脚(注释)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆