在地图

时间:2015-10-30 09:37:42

标签: swift mapkit

我似乎无法让Ray Wenderlich的mapkit教程的最后一部分工作。我可以正确显示引脚,但是当我点击引脚时,它也不会像我希望的那样在地图中打开。以下是完整教程(Link

的链接

这是我的代码:

import Foundation
import UIKit
import MapKit
import AddressBook


class CroquisMapView: UIViewController, MKMapViewDelegate {
    var section : Int?
    var index : Int?

    @IBOutlet weak var mapView: MKMapView!

    let regionRadius: CLLocationDistance = 1000
    func centerMapOnLocation(location: CLLocation) {
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
            regionRadius * 2.0, regionRadius * 2.0)
        mapView.setRegion(coordinateRegion, animated: true)
    }


    override func viewDidLoad() {
        mapView.delegate = self

        let coordenadas = croquisGruposArray[section!].items[index!].coordenadas
        let coordenadasArray = coordenadas!.characters.split{$0 == ","}.map(String.init)
        guard let lat = NSNumberFormatter().numberFromString(coordenadasArray[0])?.doubleValue,
            let long = NSNumberFormatter().numberFromString(coordenadasArray[1])?.doubleValue else {
                return
        }
        let initialLocation = CLLocation(latitude: lat, longitude: long)
        let artwork = Artwork(title: "\(croquisGruposArray[section!].items[index!].descripcion!)",
            locationName: "\(globalLigaNombre!)",
            discipline: "Futbol",
            coordinate: CLLocationCoordinate2D(latitude: lat, longitude: long))
        mapView.addAnnotation(artwork)
        centerMapOnLocation(initialLocation)
    }
}

class Artwork: NSObject, MKAnnotation {
    let title: String?
    let locationName: String
    let discipline: String
    let coordinate: CLLocationCoordinate2D

    init(title: String, locationName: String, discipline: String, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.locationName = locationName
        self.discipline = discipline
        self.coordinate = coordinate

        super.init()
    }

    var subtitle: String? {
        return locationName
    }


    func mapItem() -> MKMapItem {
        let addressDictionary = [String(kABPersonAddressStreetKey): locationName]
        let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDictionary)

        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = title

        return mapItem
    }

    func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
            print("CLICKED")
            let location = view.annotation as! Artwork
            let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
            location.mapItem().openInMapsWithLaunchOptions(launchOptions)
    }
}

extension CroquisMapView {

    // 1
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        if let annotation = annotation as? Artwork {
            let identifier = "pin"
            var view: MKPinAnnotationView
            if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
                as? MKPinAnnotationView { // 2
                    dequeuedView.annotation = annotation
                    view = dequeuedView
            } else {
                // 3
                view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                view.canShowCallout = true
                view.calloutOffset = CGPoint(x: -5, y: 5)
                view.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) as UIView
            }
            return view
        }
        return nil
    }
}

3 个答案:

答案 0 :(得分:1)

你可以试试这个:

let url  = NSURL(string: "http://maps.apple.com/?q=44.33833,13.98131")    
if UIApplication.sharedApplication().canOpenURL(url!) == true
{
    UIApplication.sharedApplication().openURL(url!)
}

答案 1 :(得分:1)

想出来:我没有在我的主类中调用calloutAccessoryControlTapped。我添加了它,并使用Andrej提供的代码:

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    let url  = NSURL(string: "http://maps.apple.com/?q=\(lat),\(long)")
    if UIApplication.sharedApplication().canOpenURL(url!) == true {
        UIApplication.sharedApplication().openURL(url!)
    }
}

答案 2 :(得分:0)

你也可以这样做:

extension ViewController : MKMapViewDelegate {

func getDirections(){      // Here you can put everything, this function is calling when the user select the dialogue pin's bubble. I propose a code who open the application Map.
    guard let selectedPin = selectedPin else { return }
    let mapItem = MKMapItem(placemark: selectedPin)
    let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
    mapItem.openInMapsWithLaunchOptions(launchOptions)
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?{

    guard !(annotation is MKUserLocation) else { return nil }
    if annotation is MKUserLocation {
        //return nil so map view draws "blue dot" for standard user location
        return nil
    }
    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
    }
    pinView?.pinTintColor = UIColor.orangeColor()
    pinView?.canShowCallout = true
    let smallSquare = CGSize(width: 30, height: 30)
    let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
    button.setBackgroundImage(UIImage(named: "car"), forState: .Normal)
    button.addTarget(self, action: #selector(self.getDirections), forControlEvents: .TouchUpInside)
    pinView?.leftCalloutAccessoryView = button

    return pinView}
}