Mapkit Pin Annotations with Swift

时间:2015-04-29 17:06:32

标签: swift mapkit

我试图用几个引脚显示世界地图。当点击左按钮时,它应该与每个与不同电话号码相关联的引脚进行电话呼叫。我四处搜索并想出了以下代码,但它调用了相同的数字。如何为每个引脚注释分配特定的电话号码?

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")!)


        }

    }

1 个答案:

答案 0 :(得分:0)

这是Pin Annotation类。我理解你在说什么,但我在编码方面太新,无法将你的观察结果转化为代码。

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
    }
}