添加按钮以打开Apple Maps以获取路线

时间:2016-08-06 06:17:00

标签: ios swift mapkit apple-maps mapkitannotation

我正在尝试添加一个按钮,该按钮将打开Apple Maps应用,并将指示该位置。非常感谢帮助!

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet var searchText: UITextField!
    var matchingItems: [MKMapItem] = [MKMapItem]()
    var destination:MKMapItem = MKMapItem()

    override func viewDidLoad() {
        super.viewDidLoad()            
        searchText.hidden = true            
    }

    @IBAction func textFieldReturn(sender: AnyObject) {
        sender.resignFirstResponder()
        mapView.removeAnnotations(mapView.annotations)            
    }

    @IBAction func fiveMiles(sender: AnyObject) {            
        changeDistanceView(8046.72)            
    }

    @IBAction func tenMiles(sender: AnyObject) {            
        changeDistanceView(16093.4)
    }

    @IBAction func fifthteenMiles(sender: AnyObject) {            
      changeDistanceView(24140.2)            
    }

    @IBAction func twentyMiles(sender: AnyObject) {            
        changeDistanceView(32186.9)            
    }

    func changeDistanceView(miles: Double) {            
        self.performSearch()
        let userLocation = mapView.userLocation

        let region = MKCoordinateRegionMakeWithDistance(
            userLocation.location!.coordinate, miles, miles)            
        mapView.setRegion(region, animated: true)

    }

    @IBAction func changeMapView(sender: UIBarButtonItem) {            
        if mapView.mapType == MKMapType.Standard {
            mapView.mapType = MKMapType.Satellite
        } else {
            mapView.mapType = MKMapType.Standard
        }

    }

    func mapView(mapView: MKMapView, didUpdateUserLocation
        userLocation: MKUserLocation) {
        mapView.centerCoordinate = userLocation.location!.coordinate

        self.performSearch()
        let userLocation = mapView.userLocation

        let region = MKCoordinateRegionMakeWithDistance(
            userLocation.location!.coordinate, 2000, 2000)

        mapView.setRegion(region, animated: true)
    }

    func performSearch() {

        matchingItems.removeAll()
        let request = MKLocalSearchRequest()
        request.naturalLanguageQuery = searchText.text
        request.region = mapView.region

        let search = MKLocalSearch(request: request)

        search.startWithCompletionHandler({
            (response: MKLocalSearchResponse?,error: NSError?) in

            if error != nil {
                print("Error occured in search: \(error!.localizedDescription)")
            } else if response!.mapItems.count == 0 {
                print("No matches found")
            } else {
                print("Matches found")

                for item in response!.mapItems {
                    print("Name = \(item.name)")
                    print("Phone = \(item.phoneNumber)")

                    self.matchingItems.append(item as MKMapItem)
                    print("Matching items = \(self.matchingItems.count)")

                    let annotation = MKPointAnnotation()
                    annotation.coordinate = item.placemark.coordinate
                    annotation.title = item.name
                    annotation.subtitle = item.phoneNumber
                    self.mapView.addAnnotation(annotation)                    
                }
            }
        })
    }

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        // Don't want to show a custom image if the annotation is the user's location.
        guard !annotation.isKindOfClass(MKUserLocation) else {
            return nil
        }

        let annotationIdentifier = "AnnotationIdentifier"

        var annotationView: MKAnnotationView?
        if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {
            annotationView = dequeuedAnnotationView
            annotationView?.annotation = annotation
        }
        else {
            let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)

            annotationView = av

            }

        if let annotationView = annotationView {
            // Configure your annotation view here
            annotationView.canShowCallout = true
            let pinImage = UIImage(named: "pawPrint.png")
            let size = CGSize(width: 30, height: 30)
            UIGraphicsBeginImageContext(size)
            pinImage!.drawInRect(CGRectMake(0, 0, size.width, size.height))
            let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

            annotationView.image = resizedImage                
        }

        return annotationView
    }       

}

1 个答案:

答案 0 :(得分:0)

试试这个希望它适合你

func openMap() {

    let lat1 : NSString = self.placeLat
    let lng1 : NSString = self.placeLng

    let latitute:CLLocationDegrees =  lat1.doubleValue
    let longitute:CLLocationDegrees =  lng1.doubleValue

    let regionDistance:CLLocationDistance = 10000
    let coordinates = CLLocationCoordinate2DMake(latitute, longitute)
    let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
    let options = [
        MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
        MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
    ]
    let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
    let mapItem = MKMapItem(placemark: placemark)
    mapItem.name = "\(self.placeName)"
    mapItem.openInMapsWithLaunchOptions(options)

}