私は、住所を取り、iPhoneにインストールされているナビゲーションアプリに対応するアクションシートを開く必要があるアプリを持っています。アクションをタップすると、指定された住所として目的地を含むナビゲーションアプリが開きます。私はそれをするチュートリアルを見つけられなかったので、いくつかのガイダンスが必要です。複数のナビゲーションアプリオプションを使用してアクションシートを表示する方法
FacebookのiOSアプリが、「道順を取得」をタップするときと同じようなものです。
私は、住所を取り、iPhoneにインストールされているナビゲーションアプリに対応するアクションシートを開く必要があるアプリを持っています。アクションをタップすると、指定された住所として目的地を含むナビゲーションアプリが開きます。私はそれをするチュートリアルを見つけられなかったので、いくつかのガイダンスが必要です。複数のナビゲーションアプリオプションを使用してアクションシートを表示する方法
FacebookのiOSアプリが、「道順を取得」をタップするときと同じようなものです。
私はあなたが開きたいすべてのナビゲーションアプリの列挙型を定義スウィフト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ファイル内のすべてのサードパーティのナビゲーションのアプリのURLスキームを追加することを忘れないでください。例えば:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
<string>here-route</string>
</array>