Google Map と Map それぞれの Custom URL Scheme
を使って経路表示をする実装をしたのでメモ程度に残しておきたいと思います👀
結論
ということで、それぞれの経路表示を Alert
として落とし込んだメソッドが下記になります。
執筆時点では、下記のような URL Scheme
で動作しますが、ここのパラメータなどは変更される可能性もあるのでご注意ください。
func showMapAppAlert(location: CLLocation) { let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) alert.addAction(.init(title: "「Map」で開く", style: .default, handler: { _ in openURL(urlString: "http://maps.apple.com/?daddr=\(location.coordinate.latitude),\(location.coordinate.longitude)&dirflg=d") })) if let checkScheme = URL(string: "comgooglemaps://"), UIApplication.shared.canOpenURL(checkScheme) { alert.addAction(.init(title: "「Google Maps」で開く", style: .default, handler: { _ in openURL(urlString: "comgooglemaps://?daddr=\(location.coordinate.latitude),\(location.coordinate.longitude)&directionsmode=driving&zoom=14") })) } alert.addAction(.init(title: "キャンセル", style: .cancel, handler: nil)) present(alert, animated: true, completion: nil) }
てな感じで本日は以上になります🍺