No MKPointAnnotation in swift

384 views Asked by At

I am trying to add a MKPointAnnotation that marks the destination. That destination is a MKMapItem that is passed from the previous view controller. 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. 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.

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

There are 1 answers

0
Pablo Picasso On BEST ANSWER

I solved this by moving the annotation from viewDidAppear to convenience init