【问题标题】:Google Maps API Directions in Swift 3Swift 3 中的 Google Maps API 方向
【发布时间】:2017-03-03 17:41:51
【问题描述】:

当我开始使用 Swift 3 时,我在使用 Google Maps API 方向构建路线时遇到了问题。

我的路线有几个航点,在 Swift 3 中的 URL:

https://maps.googleapis.com/maps/api/directions/json?origin=48.4843822562792,35.0635632500052&destination=48.4893899423081,35.0640017911792&waypoints=48.4833428800255,35.0710221379995|48.4887622031403,35.0573639944196&key=AIzaSyAWpBT3uxovKLqdWIiwa29a4AcgtspAA1k

由于“|”而不起作用。有什么建议吗?

【问题讨论】:

  • swift 3 有什么问题???它是一个与任何语言无关的纯 URL。所以我可以知道是什么问题吗?
  • 在带有 alamofire 的 swift 2.3 上,此请求按预期工作,但现在我只收到一条“腿”作为响应,尽管我发送了几个航路点
  • 您是否记录了您请求的网址?它与问题中提到的相同还是与原来的不同?
  • 是的,我在浏览器和我的应用程序中得到了不同的结果
  • 所以它破坏了你的 URL 查看你的代码,它破坏了你的 URL 字符串。

标签: ios google-maps google-maps-api-3 swift3


【解决方案1】:

在 Swift 3 中,以下内容有所帮助

let url_string = "URL STRING"
let url = URL(string:url_string.addingPercentEncoding(withAllowedCharacters:  CharacterSet.urlQueryAllowed))

【讨论】:

    【解决方案2】:

    对于那些想要使用方向 googlemaps api 的人,您必须在参数数组中发送您的航点。所以管道不会再造成问题了。

        var wayPointsString = "optimize:true"
        if waypointsForRequest.count > 0 {
            for location in waypointsForRequest {
                wayPointsString = "\(wayPointsString)|\(location.coordinate.latitude),\(location.coordinate.longitude)"
            }
        }
        let parameters : [String : String] = ["key" : self.directionsApikey, "sensor" : "false", "mode" : "driving", "alternatives" : "true", "origin" : "\(origin.coordinate.latitude),\(origin.coordinate.longitude)", "destination" : "\(destination.coordinate.latitude),\(destination.coordinate.longitude)", "waypoints" : wayPointsString]
        let url = URL(string: "https://maps.googleapis.com/maps/api/directions/json?")
    
        Alamofire.request(url!, method:.get, parameters: parameters)
            .validate(contentType: ["application/json"])
            .responseJSON { response in
                if response.value != nil {
                    let json = JSON(response.value!)
    
                }
        }
    

    这里有一个有趣的答案:https://stackoverflow.com/a/40126476/3173405

    【讨论】:

      【解决方案3】:

      嗨,我对 swift 了解不多,但我在目标 C 中遇到了同样的问题,我在下面做了一些事情并为我工作,

      NSMutableCharacterSet *alphaNumSymbols = [NSMutableCharacterSet characterSetWithCharactersInString:@"~!@#$&*()-_+=[]:;',/?."];
      [alphaNumSymbols formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
      urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:alphaNumSymbols];
      NSURL *directionsURL = [NSURL URLWithString:urlString];
      

      希望这会有所帮助

      在 Swift 中,

      var alphaNumSymbols = CharacterSet(charactersInString: "~!@#$&*()-_+=[]:;',/?.")
      alphaNumSymbols!.formUnion(CharacterSet.alphanumerics)
      urlString = urlString.addingPercentEncoding(withAllowedCharacters: alphaNumSymbols)!
      var directionsURL = NSURL(string: urlString)!
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-03
        • 1970-01-01
        • 2012-11-06
        • 2016-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-23
        相关资源
        最近更新 更多