Skip to content

Instantly share code, notes, and snippets.

@kuotinyen
Created December 21, 2018 10:22
Show Gist options
  • Save kuotinyen/a6a03a2553a98a1d347f995e2aba46ca to your computer and use it in GitHub Desktop.
Save kuotinyen/a6a03a2553a98a1d347f995e2aba46ca to your computer and use it in GitHub Desktop.
launch iOS system map app to guide location.

Usage

MapGuideManager.guide(from: nil, to:job.gps.location, with: job.companyName)

Extension

class MapGuideManager {
    class func guide(from startPoint: CLLocationCoordinate2D? = nil, to destination: CLLocationCoordinate2D? = nil, with destinationTitle: String) {
        
        guard let startPoint = startPoint else {
            let destinationMark = MKPlacemark(coordinate: destination!)
            let destinationItem = MKMapItem(placemark: destinationMark)
            destinationItem.name = destinationTitle
            
            let mapItems = [destinationItem]
            
            let dic: [String: AnyObject] = [
                MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeTransit as AnyObject,
                MKLaunchOptionsMapTypeKey: MKMapType.standard.rawValue as AnyObject,
                MKLaunchOptionsShowsTrafficKey: true as AnyObject
            ]
            
            MKMapItem.openMaps(with: mapItems, launchOptions: dic)
            
            return
        }
        
        let startPointMark = MKPlacemark(coordinate: startPoint)
        let startPointItem = MKMapItem(placemark: startPointMark)
        startPointItem.name = "現在位置"
        
        let destinationMark = MKPlacemark(coordinate: destination!)
        let destinationItem = MKMapItem(placemark: destinationMark)
        destinationItem.name = destinationTitle
        
        let mapItems = [startPointItem, destinationItem]
        
        let dic: [String: AnyObject] = [
            MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeTransit as AnyObject,
            MKLaunchOptionsMapTypeKey: MKMapType.standard.rawValue as AnyObject,
            MKLaunchOptionsShowsTrafficKey: true as AnyObject
        ]
        
        MKMapItem.openMaps(with: mapItems, launchOptions: dic)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment