[英]How do I overlay a circle at a set location using mapkit and swift
[英]How can I zoom to a particular location on a map on button click. I am using swift and MapKit
這是按鈕動作在我的視圖控制器中的外觀。
func mapButtonClickedInScrollview(sender: UIButton) {
let myWorldViewController = MyWorldViewController(nibName: "MyWorldViewController", bundle: nil)
self.navigationController?.pushViewController(myWorldViewController, animated: true)
}
這是在另一個視圖控制器(即MyWorldViewController)中設置的地圖的外觀:
[enter link description here][1]
因此,當我單擊該按鈕時,我應該轉到另一個控制器,它應該自動縮放到上圖所示的那個汽車圖釘。 任何幫助,將不勝感激!
[1]: http://i.stack.imgur.com/0Nddd.png
假設您有一個導航欄,首先創建一個從源到目標視圖控制器的推送服務,不要重復使用同一VC,因為您將需要更多的邏輯,然后為其指定一個標識符,例如, expandedAnnotation
。 然后在當前的VC MyWorldViewController
上創建一個“ selectedAnnotation”屬性
您將需要使用MapKit的didSelectAnnotationView
並找到您選擇的注釋(例如,在我的項目中,我的注釋是藝術品(自定義類名)的示例)
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
if let annotation = view.annotation as? Artwork {
selectedAnnotation = annotation
performSegueWithIdentifier("expandedAnnotation", sender: self)
}
}
然后,在您覆蓋的prepareForSegue
函數中,將注釋傳遞到目標viewcontroller。 當然,首先要在目標視圖控制器中為注釋創建一個屬性。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "expandedAnnotation") {
let destinationVC = segue.destinationViewController as! DestinationViewController
destinationVC.annotation = selectedAnnotation
}
就是這樣,在您的destinationVC viewDidLoad
,將注釋添加到mapView並將其區域居中。 祝好運
將興趣點的坐標傳遞到新的視圖控制器,並在其地圖視圖中使用setRegion
。 代碼可能看起來像這樣:
var region = MKCoordinateRegion(center: myCoordinate, span: 200)
region = mapView.regionThatFits(region)
mapView.setRegion(region, animated: true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.