0

I am trying to display a map of the world with several pins. When the left button is tapped, it should perform a phone call with each pin associated with a different phone number. I searched around and came up with the following code but it calls the same number. How can I assign a specific phone number to each pin annotation?

import UIKit
import MapKit
import CoreLocation



class CrewDesksViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!

    @IBOutlet weak var callCrew: UISegmentedControl!


  let locationManager = CLLocationManager()


    override func viewDidLoad() {
        super.viewDidLoad()



        // Ask for Authorisation from the User.
        self.locationManager.requestAlwaysAuthorization()

        // For use in foreground
        self.locationManager.requestWhenInUseAuthorization()

        if CLLocationManager.locationServicesEnabled() {
//            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()

        }
        //Setup our Map View



        self.mapView.delegate = self


        mapView.showsUserLocation = true


    // pin location of different phone numbers for contacting the crewdesk 
        var location : CLLocationCoordinate2D = CLLocationCoordinate2DMake(50.1166667, 8.6833333)
        let pinAnnotation = PinAnnotation()
        pinAnnotation.setCoordinate(location)
        pinAnnotation.title = "Frankfurt"
        pinAnnotation.subtitle = "+496969804620"
        self.mapView.addAnnotation(pinAnnotation)

        var location2 : CLLocationCoordinate2D = CLLocationCoordinate2DMake(41.8781136, -87.6297982)
        let pinAnnotation2 = PinAnnotation()
        pinAnnotation2.setCoordinate(location2)
        pinAnnotation2.title = "USA"
        pinAnnotation2.subtitle = "+1800FLTLINE"
        self.mapView.addAnnotation(pinAnnotation2)

        var location3 : CLLocationCoordinate2D = CLLocationCoordinate2DMake(48.866667, 2.333333)
        let pinAnnotation3 = PinAnnotation()
        pinAnnotation3.setCoordinate(location3)
        pinAnnotation3.title = "Paris"
        pinAnnotation3.subtitle = "+33800900814"
        self.mapView.addAnnotation(pinAnnotation3)

        var location4 : CLLocationCoordinate2D = CLLocationCoordinate2DMake(51.5085300, -0.1257400)
        let pinAnnotation4 = PinAnnotation()
        pinAnnotation4.setCoordinate(location4)
        pinAnnotation4.title = "London"
        pinAnnotation4.subtitle = "+44800896516"
        self.mapView.addAnnotation(pinAnnotation4)

        var location5 : CLLocationCoordinate2D = CLLocationCoordinate2DMake(50.833333, 4.333333)
        let pinAnnotation5 = PinAnnotation()
        pinAnnotation5.setCoordinate(location5)
        pinAnnotation5.title = "Brussels"
        pinAnnotation5.subtitle = "+3980019326"
        self.mapView.addAnnotation(pinAnnotation5)


        var location6 : CLLocationCoordinate2D = CLLocationCoordinate2DMake(35.685, 139.7513889)
        let pinAnnotation6 = PinAnnotation()
        pinAnnotation6.setCoordinate(location6)
        pinAnnotation6.title = "Japan 日本"
        pinAnnotation6.subtitle = "0-0531-12-4066"
        self.mapView.addAnnotation(pinAnnotation6)

        var location7 : CLLocationCoordinate2D = CLLocationCoordinate2DMake(22.2855200, 114.1576900)
        let pinAnnotation7 = PinAnnotation()
        pinAnnotation7.setCoordinate(location7)
        pinAnnotation7.title = "HongKong"
        pinAnnotation7.subtitle = "800-96-5483"
        self.mapView.addAnnotation(pinAnnotation7)


        var location8 : CLLocationCoordinate2D = CLLocationCoordinate2DMake(52.3740300, 4.8896900)
        let pinAnnotation8 = PinAnnotation()
        pinAnnotation8.setCoordinate(location8)
        pinAnnotation8.title = "Amsterdam"
        pinAnnotation8.subtitle = "0-800-022-9324"
        self.mapView.addAnnotation(pinAnnotation8)


        var location9 : CLLocationCoordinate2D = CLLocationCoordinate2DMake(13.75, 100.516667)
        let pinAnnotation9 = PinAnnotation()
        pinAnnotation9.setCoordinate(location9)
        pinAnnotation9.title = "Bangkok"
        pinAnnotation9.subtitle = "01-800-12-066-6078"
        self.mapView.addAnnotation(pinAnnotation9)

        var location10 : CLLocationCoordinate2D = CLLocationCoordinate2DMake(29.3375, 47.6581)
        let pinAnnotation10 = PinAnnotation()
        pinAnnotation10.setCoordinate(location10)
        pinAnnotation10.title = "Kuwait"
        pinAnnotation10.subtitle = "00-1-847-700-9893"
        self.mapView.addAnnotation(pinAnnotation10)

        var location11 : CLLocationCoordinate2D = CLLocationCoordinate2DMake(1.3667, 103.8)
        let pinAnnotation11 = PinAnnotation()
        pinAnnotation11.setCoordinate(location11)
        pinAnnotation11.title = "Singapore"
        pinAnnotation11.subtitle = "800-1204657"
        self.mapView.addAnnotation(pinAnnotation11)

        var location12 : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-33.8830555556, 151.216666667)
        let pinAnnotation12 = PinAnnotation()
        pinAnnotation12.setCoordinate(location12)
        pinAnnotation12.title = "Sydney"
        pinAnnotation12.subtitle = "1-800-1-48557"
        self.mapView.addAnnotation(pinAnnotation12)


        var location13 : CLLocationCoordinate2D = CLLocationCoordinate2DMake(25.0391667, 121.525)
        let pinAnnotation13 = PinAnnotation()
        pinAnnotation13.setCoordinate(location13)
        pinAnnotation13.title = "Taipei"
        pinAnnotation13.subtitle = "0-0801-13-8533"
        self.mapView.addAnnotation(pinAnnotation13)


    }

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        if annotation is PinAnnotation {
            let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")

            pinAnnotationView.pinColor = .Purple
            pinAnnotationView.draggable = true
            pinAnnotationView.canShowCallout = true
            pinAnnotationView.animatesDrop = true

            let deleteButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
            deleteButton.frame.size.width = 44
            deleteButton.frame.size.height = 44

            deleteButton.setImage(UIImage(named: "appiconround"), forState: .Normal)

            pinAnnotationView.leftCalloutAccessoryView = deleteButton

            return pinAnnotationView
        }

        return nil
    }

    func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {

        if let location = view.annotation as? PinAnnotation {

            UIApplication.sharedApplication().openURL(NSURL(string: "tel://+1800FLTLINE")!)


        }

    }
Aaron
  • 5,875
  • 6
  • 33
  • 69
birkyboy
  • 61
  • 2
  • 10
  • In calloutAccessoryControlTapped, instead of using a hard-coded string, create a string combining the tel prefix and the location's subtitle. Read the [Strings and Characters section of the Swift guide](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-ID285). See http://stackoverflow.com/a/29869456/467105 for an example which uses "string interpolation". However, here you could even just do simple string concatenation like `let telUrlString = "tel://" + location.subtitle`. –  Apr 30 '15 at 12:05
  • Thank You very much it works very well. Such a simple solution. – birkyboy May 01 '15 at 12:47

2 Answers2

0

You're using a class PinAnnotation but didn't provide us with the definition of that class.

I'll assume it's some object that conforms to the MKAnnotation protocol.

You're saving your phone numbers in the subtitle property of your PinAnnotation objects.

In your calloutAccessoryControlTapped method, you get passes the annotation view that the user tapped.

The MKAnnotationView class has a property annotation that holds the annotation object associated with the annotation view.

You need to fetch the annotation from the annotation view, load the subtitle property (which is part of the MKAnnotation protocol) and use that as the phone number to place your phone call.

Duncan C
  • 115,063
  • 19
  • 151
  • 241
0

This is the Pin Annotation class. I understand what you are saying but i am too new in coding to actually translate your observations into code.

import Foundation
import UIKit
import MapKit

class PinAnnotation : NSObject, MKAnnotation {
    private var coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)

    var coordinate: CLLocationCoordinate2D {
        get {
            return coord
        }
    }

    var title: String = ""
    var subtitle: String = ""

    func setCoordinate(newCoordinate: CLLocationCoordinate2D) {
        self.coord = newCoordinate
    }
}
birkyboy
  • 61
  • 2
  • 10