Skip to content

Instantly share code, notes, and snippets.

@mavieth
Created January 6, 2020 19:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mavieth/ca7506f2c2e20e903ac2e246e3be254c to your computer and use it in GitHub Desktop.
Save mavieth/ca7506f2c2e20e903ac2e246e3be254c to your computer and use it in GitHub Desktop.
//
// 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