我刚刚用GUI创建了CustomCallout.xib . 我想将它用作我的注释的自定义标注,但我不知道应该在哪里设置要显示的视图以及如何将值传递给该视图 .
我要显示的 callout view 看起来像这样 .
这是我的 custom annotation 的代码 .
class MapPin : MKPointAnnotation {
var name: String
var street: String
var type: String
var postCode: String
init(name: String, street: String, type: String, postCode: String) {
self.name = name
self.street = street
self.type = type
self.postCode = postCode
}}
我的 view controller :
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var mapView: MKMapView!
let span = MKCoordinateSpanMake(0.05, 0.05)
override func viewDidLoad() {
super.viewDidLoad()
self.mapView.delegate = self
let location = CLLocationCoordinate2D(
latitude: 53.430,
longitude: 14.529)
let region = MKCoordinateRegion(center: location, span: span)
mapView.setRegion(region, animated: true)
let punkt = MapPin(name: "rand", street: "rand", type: "rand", postCode: "rand")
punkt.coordinate = location
mapView.addAnnotation(punkt)
}
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
if view.annotation.isKindOfClass(MKUserLocation){
return
}
var customView = (NSBundle.mainBundle().loadNibNamed("CustomCallout", owner: self, options: nil))[0] as! CustomCallout;
var calloutViewFrame = customView.frame;
calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
customView.frame = calloutViewFrame;
let cpa = view.annotation as! MapPin
view.addSubview(customView)
var newRegion = MKCoordinateRegion(center:cpa.coordinate, span: span)
self.mapView.setRegion(newRegion, animated: true)
}}
不幸的是,正在运行的应用程序显示正确创建的pin具有正确的位置,但 let punkt = MapPin(name: "rand", street: "rand", type: "rand", postCode: "rand")
中的值未传递给视图,因此它看起来像这样 .
1 回答
你可以在Swift中显示这样的自定义标注视图 .