I have just created CustomCallout.xib with a GUI. I would like to use it as a custom callout for my annotations but I don't know where I should set that view to be displayed and how to pass values to that view.
The callout view which I would like to display looks like this.
Here is the code for my 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
}}
And my 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)
}}
Unfortunately, running app shows properly created pin with correct location but values from let punkt = MapPin(name: "rand", street: "rand", type: "rand", postCode: "rand")
aren't passed to the view, so it looks like this.
you can show custom callout view like this in Swift.
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKPinAnnotationView!)
{
if view.annotation.isKindOfClass(MKUserLocation){
return
}
var customView = (NSBundle.mainBundle().loadNibNamed("SubView", owner: self, options: nil))[0] as CustomSubView;
var calloutViewFrame = customView.frame;
calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
customView.frame = calloutViewFrame;
let cpa = view.annotation as CustomPointAnnotation
//You can add set vlaues for here
//cpa.title
//cpa.postCode
//cpa.street
view.addSubview(customView)
//zoom map to show callout
let spanX = 0.0000000000000001
let spanY = 0.0000000000000001
var newRegion = MKCoordinateRegion(center:cpa.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
self.map?.setRegion(newRegion, animated: true)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With