我正在尝试添加标记目标的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)
最佳答案
我通过将注释从viewDidAppear
移动到convenience init
解决了这个问题
关于ios - 快速没有MKPointAnnotation,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30897090/