我設法得到一個自定義圖標的註釋在Swift中,但現在我仍然堅持使用2個不同的圖像爲不同的註釋。現在,一個按鈕爲地圖添加註釋。應該有另一個按鈕,也添加了註釋,但與另一個圖標。Swift不同的圖像的註釋
有沒有辦法使用reuseId這個?
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var Map: MKMapView!
@IBAction func btpressed(sender: AnyObject) {
var lat:CLLocationDegrees = 40.748708
var long:CLLocationDegrees = -73.985643
var latDelta:CLLocationDegrees = 0.01
var longDelta:CLLocationDegrees = 0.01
var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long)
var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
Map.setRegion(region, animated: true)
var information = MKPointAnnotation()
information.coordinate = location
information.title = "Test Title!"
information.subtitle = "Subtitle"
Map.addAnnotation(information)
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if !(annotation is MKPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.image = UIImage(named:"1.png")
anView.canShowCallout = true
}
else {
anView.annotation = annotation
}
return anView
}
的碼大位。謝謝。 – 2015-03-06 19:55:17
簡單而直接+1 – 2015-03-31 09:15:54
只需要注意一下,您需要實現協議'MKMapViewDelegate'並將您的控制器配置爲func mapView(mapView:MKMapView !,您可以在故事板中或手動編寫代碼) viewForAnnotation註解:MKAnnotation!) - > MKAnnotationView! '工作的方法。這使我絆倒了一下。 – 2015-04-06 17:00:33