简体   繁体   English

快速没有MKPointAnnotation

[英]No MKPointAnnotation in swift

I am trying to add a MKPointAnnotation that marks the destination. 我试图添加一个标记目标的MKPointAnnotation。 That destination is a MKMapItem that is passed from the previous view controller. destination是从以前的视图控制器传递的MKMapItem I know that the destination has a value because the CLCircularRegion right below it uses the destination for the center and fires once the user is within range. 我知道destination具有值,因为它下方的CLCircularRegion使用destination为中心,并在用户处于范围内时触发。 Is there anything wrong with the way I'm adding the annotation? 我添加注释的方式有什么问题吗?

import UIKit
import MapKit
import CoreLocation

class ViewController2: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{

var map: MKMapView?
var manager: CLLocationManager?
var destination: MKMapItem?


convenience init(frame:CGRect, destination:MKMapItem){
    self.init(nibName: nil, bundle: nil)
    self.destination = destination
    self.view.frame = frame


    self.map = MKMapView(frame: frame)
    self.map!.delegate = self

    self.view.addSubview(self.map!)
}


func viewDidAppear(animated: Bool) {

        var annotation = MKPointAnnotation()
        annotation.coordinate = destination!.placemark.coordinate
        self.map!.addAnnotation(annotation)

        var center: CLLocationCoordinate2D = destination!.placemark.coordinate
        var radius: CLLocationDistance = CLLocationDistance(300)
        var identifier: String = "Destination"
        let region = CLCircularRegion(center: center, radius: radius, identifier: identifier)
        manager?.startMonitoringForRegion(region)

    }

The provideDirections function is called when a button is pressed. 当按下按钮时,将调用provideDirections函数。

import UIKit
import MapKit
import Parse

class ViewController1: UIViewController {

var alat = ""
var alng = ""


func provideDirections(){

                self.manager = CLLocationManager()
                self.manager.startUpdatingLocation()
                println("Location Updated")

                let request = MKDirectionsRequest()
                request.setSource(MKMapItem.mapItemForCurrentLocation())

                let latitude = (alat as NSString).doubleValue
                let longitude = (alng as NSString).doubleValue

                let destinationCoordinate = MKPlacemark(coordinate: CLLocationCoordinate2DMake(latitude, longitude), addressDictionary: nil)

                var secondVC = ViewController2(frame: self.view.frame, destination: MKMapItem(placemark: destinationCoordinate))


                self.navigationController?.pushViewController(secondVC, animated: true)

我通过将注释从viewDidAppear移到了convenience init来解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM