Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set .xib interface as a custom callout view?

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. enter image description here

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. enter image description here

like image 606
wtznc Avatar asked Apr 22 '15 11:04

wtznc


1 Answers

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)
   }
like image 91
Amol Pimpare Avatar answered Nov 03 '22 13:11

Amol Pimpare