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

时间:2022-10-30 14:35:00

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

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

var MyPins: MKPinAnnotatoinView!

var MyPins:MKPinAnnotatoinView!

......

......

override func viewDidLoad() {
    super.viewDidLoad()

Mapview code

Mapview代码

..... ..... }

..... .....

2 个解决方案

#1


38  

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添加到项目中。

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

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.创建Storyboard变量IBOutlet的地图视图控件或在视图控制器中创建它。设置此变量的委托以覆盖它的委托方法:

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.

您可以在地图视图中将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)
}

#2


0  

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

您需要为用户需要添加引脚的时间和位置调用方法。如果你想让它添加一个用户点击并保持在地图上的引脚,你需要调用一个gestureRecognizer,但是如果你想通过一个按钮来做,你显然只是在一个动作中调用它。无论哪种方式,添加引脚的文档都在这里进行了详细讨论

#1


38  

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添加到项目中。

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

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.创建Storyboard变量IBOutlet的地图视图控件或在视图控制器中创建它。设置此变量的委托以覆盖它的委托方法:

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.

您可以在地图视图中将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)
}

#2


0  

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

您需要为用户需要添加引脚的时间和位置调用方法。如果你想让它添加一个用户点击并保持在地图上的引脚,你需要调用一个gestureRecognizer,但是如果你想通过一个按钮来做,你显然只是在一个动作中调用它。无论哪种方式,添加引脚的文档都在这里进行了详细讨论