简体   繁体   中英

Swift: App needs to open Apple Maps when Google Maps not installed

I have built an app for the Copenhagen bikers but have some trouble. The app needs to open Apple maps when Google Maps is not available on the phone.

The code looks like this

// 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)},

When tapping the Info-window, navigation for Google Maps needs to appear (it works). However, if the Google Maps app is not available, how do I re-write the "Marker Functions" so it will open i Apple Maps instead?

To do this, you will need to check whether Google Maps is installed. This can be done fairly easy:

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)
    }
}

Per default iOS does not allow checking whether an URL can be opened for privacy reasons. You need to whitelist checking for comgooglemaps:// in your app. To do this add this to your apps Info.plist :

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>comgooglemaps</string>
</array>

If you have done this correctly openMaps(latitude:, logitude:) will open Google Maps if it is installed, otherwise it will open Apple Maps. Just change your closures ("Marker Functions") to call the new method.

Of course you will also need to import MapKit for this to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM