1

我正在尝试使用 Google API 获取路线数据,但它一直在崩溃。这是我的代码:

let baseURLDirections = "https://maps.googleapis.com/maps/api/directions/json?"
var selectedRoute: Dictionary<NSObject, AnyObject>!
var overviewPolyline: Dictionary<NSObject, AnyObject>!
var originCoordinate: CLLocationCoordinate2D!
var destinationCoordinate: CLLocationCoordinate2D!
var originAddress: String!
var destinationAddress: String!

func getDirections(origin: String, destination: String, waypoints: Array<String>!, travelMode: AnyObject!, completionHandler: ((status: String, success: Bool) -> Void)) {

    var directionsURLString = baseURLDirections + "origin=" + origin + "&destination=" + destination

    directionsURLString = directionsURLString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!

    let directionsURL = NSURL(string: directionsURLString)
    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: directionsURL!)
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(urlRequest) {
        (data, response, error) -> Void in

        do {
            let directionsData = NSData(contentsOfURL: directionsURL!)
            if let dictionary: Dictionary<NSObject, AnyObject> = try NSJSONSerialization.JSONObjectWithData(directionsData!, options: .MutableContainers) as? Dictionary<NSObject, AnyObject> {

                let status = dictionary["status"] as! String

                if status == "OK" {
                    self.selectedRoute = (dictionary["routes"] as! Array<Dictionary<NSObject, AnyObject>>)[0]

                    self.overviewPolyline = self.selectedRoute["overview_polyline"] as! Dictionary<NSObject, AnyObject>

                    let legs = self.selectedRoute["legs"] as! Array<Dictionary<NSObject, AnyObject>>

                    let startLocationDictionary = legs[0]["start_location"] as! Dictionary<NSObject, AnyObject>
                    self.originCoordinate = CLLocationCoordinate2DMake(startLocationDictionary["lat"] as! Double, startLocationDictionary["lng"] as! Double)

                    let endLocationDictionary = legs[legs.count - 1]["end_location"]as! Dictionary<NSObject, AnyObject>
                    self.destinationCoordinate = CLLocationCoordinate2DMake(endLocationDictionary["lat"] as! Double, endLocationDictionary["lng"] as! Double)

                    self.originAddress = legs[0]["start_address"]as! String
                    self.destinationAddress = legs[legs.count - 1]["end_address"]as! String

                    self.calculateTotalDistanceAndDuration()

                    completionHandler(status: status, success: true)
                }
                else {
                    completionHandler(status: status, success: false)
                }
            }
        } catch let error as NSError {
            print(error)
            completionHandler(status: "", success: false)
        }
    }

    task.resume()
}

它说“EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)”

self.overviewPolyline = self.selectedRoute["overview_polyline"] as! Dictionary<NSObject, AnyObject>

即使我删除了里面的所有代码

if let dictionary: Dictionary<NSObject, AnyObject> = try NSJSONSerialization.JSONObjectWithData(directionsData!, options: .MutableContainers) as? Dictionary<NSObject, AnyObject> {

我收到 EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)。我该如何解决这个问题?

4

2 回答 2

1

当我改变时它起作用了

directionsURLString = directionsURLString.stringByAddingPercentEncodingWithAllowedCharacters(.UR>LHostAllowedCharacterSet())!

directionsURLString = directionsURLString.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())!

感谢您为我指明正确的方向!在 swift 3.0 中,它转换为:

SearchString = SearchString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!

这是另一个用于 URL 准备的有用的字符串修饰符(删除重音等,但不是像“ø”这样的奇怪字符):

SearchString = SearchString.folding(options: .diacriticInsensitive, locale: .current)
于 2017-03-19T16:54:30.807 回答
0

当我改变时它起作用了

directionsURLString = directionsURLString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!

directionsURLString = directionsURLString.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())!
于 2016-08-08T17:44:13.663 回答