[英]Ionic 3 - Open geolocation without navigation with Google Maps / Apple Maps app
[英]Swift: App needs to open Apple Maps when Google Maps not installed
我为哥本哈根骑自行车的人构建了一个应用程序,但遇到了一些麻烦。 当手机上没有Google Maps时,该应用需要打开Apple地图。
代码看起来像这样
// OUTLETS!
@IBOutlet weak var googleMapsView: GMSMapView!
// VARIABLES!
var locationManager = CLLocationManager()
var M1: GMSMarker!
var M2: GMSMarker!
//Marker funtioncs
var markerFunctions: [GMSMarker: (() -> Void)]!
override func viewDidLoad() {
super.viewDidLoad()
// GET LOCATION WHILE USING APP
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.startMonitoringSignificantLocationChanges()
initGoogleMaps()
}
// START GOOGLE MAPS!
func initGoogleMaps() {
// MARKERS
M1 = GMSMarker()
M1.position = CLLocationCoordinate2D(latitude: 55.65208178045790, longitude: 12.527047696518300)
M1.title = "Sjælør St."
M1.snippet = "Press for navigation"
M1.map = self.googleMapsView
M2 = GMSMarker()
M2.position = CLLocationCoordinate2D(latitude: 55.67530850095370, longitude: 12.583165039072400)
M2.title = "Børsen (Slotsholmsgade)"
M2.snippet = "Press for navigation"
M2.map = self.googleMapsView
// Marker functions
markerFunctions = [
M1: { UIApplication.shared.open(NSURL(string: "comgooglemaps-x-callback://" +
"?daddr=55.65208178045790,12.527047696518300&directionsmode=bicycling&zoom=17")! as URL)},
M2: { UIApplication.shared.open(NSURL(string: "comgooglemaps-x-callback://" +
"?daddr=55.67530850095370,12.583165039072400&directionsmode=bicycling&zoom=17")! as URL)},
点击“信息”窗口时,需要显示Google Maps的导航(有效)。 但是,如果Google Maps应用程序不可用,如何重新编写“标记功能”,以便它可以打开i Apple Maps?
为此,您需要检查是否已安装Google Maps。 这可以很容易地完成:
func openMaps(latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
let googleMapsInstalled = UIApplication.shared.canOpenURL(URL(string: "comgooglemaps://")!)
if googleMapsInstalled {
UIApplication.shared.open(URL(string: "comgooglemaps-x-callback://" +
"?daddr=\(latitude),\(longitude)&directionsmode=bicycling&zoom=17")!)
} else {
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.openInMaps(launchOptions: nil)
}
}
默认情况下,iOS不允许检查出于隐私原因是否可以打开URL。 您需要将应用中的comgooglemaps://
列入白名单。 为此,请将其添加到您的应用程序Info.plist
:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
</array>
如果您已正确openMaps(latitude:, logitude:)
此操作,则openMaps(latitude:, logitude:)
将打开Google Maps(如果已安装),否则它将打开Apple Maps。 只需更改您的闭包(“标记功能”)即可调用新方法。
当然,您还需要import MapKit
才能起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.