Created
January 6, 2020 19:08
-
-
Save mavieth/ca7506f2c2e20e903ac2e246e3be254c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MapHelper.swift | |
// | |
// Created by Michael Vieth on 1/6/20. | |
// | |
import Foundation | |
import GoogleMaps | |
import GoogleMapsBase | |
import MapKit | |
import UIKit | |
public struct MapHelper { | |
static func openGoogleDirections(address: String) { | |
// Check if Google Maps has been installed on device | |
let canUseGoogleApp = UIApplication.shared.canOpenURL(URL(string:"comgooglemaps-x-callback://")!) | |
// Use Google Maps HTTP URL by default | |
var scheme = "https" | |
var host = "www.google.co.in" | |
var path = "/maps/dir/" | |
let saddr = URLQueryItem(name: "saddr", value: "") | |
let daddr = URLQueryItem(name: "daddr", value: address) | |
if canUseGoogleApp { | |
// Use the Google Maps App URL | |
scheme = "comgooglemaps" | |
path = "" | |
host = "" | |
} | |
// Build URL + Query Items | |
var urlComponents = URLComponents() | |
urlComponents.scheme = scheme | |
urlComponents.host = host | |
urlComponents.path = path | |
urlComponents.queryItems = [saddr, daddr] | |
// Open directions URl | |
if let url = urlComponents.url { | |
UIApplication.shared.open(url) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment