2017-05-24 3 views
0

거리 주소를 사용하고 iPhone에 설치된 탐색 앱에 해당하는 작업이있는 작업 시트를 열어야하는 앱이 있습니다. 작업을 탭하면 목적지와 제공된 길 찾기 주소로 탐색 앱이 열립니다. 튜토리얼을 찾지 못 했으므로 지침이 필요합니다.여러 내비게이션 앱 옵션을 사용하여 작업 시트를 표시하는 방법

페이스 북의 iOS 앱이 "길 찾기"를 두드리는 것과 비슷합니다.

답변

0

나는 열려있는 모든 네비게이션 애플 리케이션을위한 열거를 정의

스위프트 4에서 그것을 구현했습니다.

enum NavigationApps: String { 
    case appleMaps = "Maps" 
    case googleMaps = "Google Maps" 
    case hereWeGo = "HERE WeGo" 
} 

하기 방법은 위치 튜플 위도 및 경도 파라미터를 열거하고 상기 존재 및 사용자 디바이스에 설치된 모든지도 옵션 작업 시트를 연다. installedNavigationApps은 키가 NavigationApps 열거 형이고 값이 각각의 URL 스키마 인 사전 (키/값 쌍)의 배열입니다.

NavigationApps 열거 형의 모든 탐색 앱, installedNavigationApps 사전의 URL 스키마 및 각 탐색 앱의 앱 실행을 UIAlertAction 처리기로 처리해야한다고 말하면됩니다. 중요

// MARK: - Map Navigation 

func openMapForLocation(location: (latitude: CLLocationDegrees, longitude: CLLocationDegrees)) { 

    let installedNavigationApps : [[String:String]] = [[NavigationApps.appleMaps.rawValue:""], [NavigationApps.googleMaps.rawValue:"comgooglemaps://"], [NavigationApps.hereWeGo.rawValue:"here-route://"]] 

    var alertAction: UIAlertAction? 

    let alert = UIAlertController(title: "Select Navigation App", message: "Open in", preferredStyle: .actionSheet) 

    for app in installedNavigationApps { 
     if (app.keys.first == NavigationApps.appleMaps.rawValue || UIApplication.shared.canOpenURL(URL(string:app[app.keys.first!]!)!)) 
     { 
      let appName = app.keys.first 

      alertAction = UIAlertAction(title: appName, style: .default, handler: { (action) in 
       switch appName { 
       case NavigationApps.appleMaps.rawValue?: 
        let regionDistance:CLLocationDistance = 10000 
        let coordinates = CLLocationCoordinate2DMake(location.latitude, location.longitude) 
        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 = "Destination" 
        mapItem.openInMaps(launchOptions: options) 
        break 

       case NavigationApps.googleMaps.rawValue?: 
        UIApplication.shared.open(NSURL(string: 
         "comgooglemaps://?saddr=&daddr=\(location.latitude),\(location.longitude)&directionsmode=driving")! as URL, options: [:], completionHandler: nil) 
        break 

       case NavigationApps.hereWeGo.rawValue?: 
        UIApplication.shared.open(NSURL(string: 
         "here-route://mylocation/\(location.latitude),\(location.longitude)?ref=Destination&m=d")! as URL, options: [:], completionHandler: nil) 
        break 

       default: 
        break 
       } 
      }) 
      alert.addAction(alertAction!) 
     } 
     else 
     { 
      print("Can't open URL scheme") 
     } 
    } 

    alertAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 
    alert.addAction(alertAction!) 

    self.present(alert, animated: true, completion: nil) 
} 

: 는의 Info.plist에 모든 제 3 자 탐색 응용 프로그램의 URL 방식을 추가하는 것을 잊지 마십시오. 예 :

<key>LSApplicationQueriesSchemes</key> 
<array> 
<string>comgooglemaps</string> 
<string>here-route</string> 
</array> 
관련 문제