我正在尝试添加标记目的地的MKPointAnnotation。 destination
是从前一个视图控制器传递的MKMapItem
。我知道destination
有一个值,因为它下面的CLCircularRegion
使用destination
作为中心,一旦用户在范围内就会触发。我添加注释的方式有什么问题吗?
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)
}
按下按钮时会调用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)
答案 0 :(得分:0)
我通过将注释从viewDidAppear
移到convenience init