I am trying to get directions using the Google directions api. Everything I have done so far works. I have search functionality that saves the searched location (using Google's suggested locations) then it places a marker there.
I just added some code that will use the Google Directions API to draw out the map from a specific location (in this case, my current location). I have no trouble with my app keeping track of my location and everything.
I keep getting the "error: cannot create URL" error in my guard statement when I try to create the URL I have attached a screenshot below, but again i'm slightly confused. Where it says key= that's where i'm entering my API key. I obviously didn't include that in this screenshot as that would give everyone access to my API key.
I went ahead and actually used the printed urlString (that displays in console) and copied the URL to my web page and it worked perfectly. It showed what I want it to show (as far as I know) and even tried without the key in the web page and it still worked. Has anyone run into this issue? I already did the transport security domain exception so there's no issue there.
I have attached the code below. both currentLatitude/Longtitude and searchedLatitude/Longitude are of type CLLocationDegrees. Current being my current location coordinates and searched being the coorinates that are returned back to me when user searches a location
let currLat = String(currentLatitude)
let currLong = String(currentLongitude)
let seLat = String(searchedLatitude)
let seLong = String(searchedLongitude)
let originCoordinateString = "\(currLat), \(currLong)"
let destinationCoordinateString = "\(seLat), \(seLong)"
print(currentLatitude)
print(currentLongitude)
print(searchedLatitude)
print(searchedLongitude)
let urlString = "http://maps.googleapis.com/maps/api/directions/json?origin=\(originCoordinateString)&destination=\(destinationCoordinateString)&sensor=false"
print(urlString)
guard let url = URL(string: urlString) else {
print(urlString)
print ("Error: cannot create URL")
return
}