-1

I am trying to implement a method that uses Googles snap to roads API however I have been unable to achieve any results.

I was able to successfully implement the directions API in to my Swift project using

https://maps.googleapis.com/maps/api/directions/json?origin=xyz

let request = NSURLRequest(URL: NSURL(string:directionURL)!)
    let session = NSURLSession.sharedSession()
    session.dataTaskWithRequest(request,
        completionHandler: {(data: NSData!, response: NSURLResponse!, error: NSError!) in

            if error == nil {
                let object = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as! NSDictionary
                println(object)

                let routes = object["routes"] as! [NSDictionary]
                for route in routes {
                    println(route["summary"])
                }
                dispatch_async(dispatch_get_main_queue()) {
                    //update your UI here
                }
            }
            else {
                println("Direction API error")
            }

    }).resume()

however if the GPS coordinates are even slightly different I get an entirely different result.

What I am trying to do is plot a users path the same each time even if the start/end coordinates a slightly different.

The error I am getting is

fatal error: unexpectedly found nil while unwrapping an Optional value

Any suggestions?

Thanks

EDIT:

I am trying this but this is what is causing the error

func directionAPITest() {

    let directionURL = "https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907|-35.28099,149.12929|-35.28144,149.12984|-35.28194,149.13003|-35.28282,149.12956|-35.28302,149.12881|-35.28473,149.12836&interpolate=true&key=xyz"

    let request = NSURLRequest(URL: NSURL(string:directionURL)!)
    let session = NSURLSession.sharedSession()

}
puks1978
  • 3,667
  • 11
  • 44
  • 103
  • Are you sure that JSONObjectWithData is a NSDictionary? (and not an Array) Can you add a breakpoint to that line and inspect the data without using explicit casting. Is that data also nil? – Konrad77 Jul 20 '15 at 08:16
  • Are you getting a crash or "entirely different results"? Please explain your problem more clearly. – Mundi Jul 20 '15 at 09:12
  • Yes I am getting a crash. I have added the error. I just want to be able to is the roads api, not the directions api – puks1978 Jul 20 '15 at 20:37
  • What line did the error fall on? – Code Different Jul 20 '15 at 21:10
  • I have added the snaptoroads function which is causing the error. I don't even know if I am using it correctly in Swift. – puks1978 Jul 21 '15 at 00:35

1 Answers1

0

I was able to resolve this after finding this post NSURL found nil while unwraping an Optional value

Basically had to escape the string.

Community
  • 1
  • 1
puks1978
  • 3,667
  • 11
  • 44
  • 103