insta catering
insta catering

Reputation: 160

Google Directions Api URL returning nil

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
    }

Upvotes: 2

Views: 359

Answers (2)

Nerdy Bunz
Nerdy Bunz

Reputation: 7497

The reason that

url = URL(string: urlString)

fails is because the urlString contains spaces.

Technically, URLs are formatted with a strict standard.

The browser accepts it as-is because it automatically fixes it before sending.

In your case you can just remove the spaces in your originCoordinateString and destinationCoordinateString variables.

  • Boober.

Upvotes: 2

Patru
Patru

Reputation: 4551

As I told you: you should make your code runable in a Playground "as is" (which your code is not).

This is easy enough to fix at this point however, so here it goes anyways. This will not run:

let originCoordinateString = "41, 23"
let destinationCoordinateString = "41, 24"
let urlString = "http://maps.googleapis.com/maps/api/directions/json?origin=\(originCoordinateString)&destination=\(destinationCoordinateString)&sensor=false"

print(urlString)

if let url = URL(string: urlString) {
    print(url)
}else {
    print(urlString)
    print ("Error: cannot create URL")
}

This will

let originCoordinateString = "41,23"
let destinationCoordinateString = "41,24"
let urlString = "http://maps.googleapis.com/maps/api/directions/json?origin=\(originCoordinateString)&destination=\(destinationCoordinateString)&sensor=false"

print(urlString)

if let url = URL(string: urlString) {
    print(url)
}else {
    print(urlString)
    print ("Error: cannot create URL")
}

So your problem has nothing to do with JSON in the first place, but with your creation of the URL. Granted: the error "message" of the URL initializer is minimal at best, but there is petty little it can do (short of throwing an exception which is not nice either). Spaces are a big noo noo there. If you have to put them (clearly not in this case) they should be escaped, either with + or with %20 depending on the application, but they should be avoided wherever possible.

Upvotes: 0

Related Questions