谷歌地图API搜索请求没有英语地方(SWIFT)



当我尝试在浏览器上获取商店的坐标时,使用此链接获得 json 结果没有错误:

"https://maps.googleapis.com/maps/api/place/radarsearch/json?location=24.712587,46.673184&radius=5000&name=ستاربكس&key=#####"

但是当我在Xcode中通过Alamofire进行HTTP请求时

let url = "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=24.712587,46.673184&radius=5000&name=ستاربكس&key=####"
    let  requestURL =
    Alamofire.request(url).responseJSON
        { response in
            print(response.result.debugDescription)
            print(response.request)  // original URL request
            print(response.response)
            print(response.data)     // server data
            print(response.result)   // result of response serialization
            if let JSON = response.result.value {
                print("JSON: (JSON)")
            }
    }
    }

我对所有打印都为零!为什么要这样做?但是,当我请求通过 Alamofire 搜索英文名称时,它可以工作!

我的猜测是苹果框架中的东西被拉丁和阿拉伯字符的混合所绊倒。您可以通过分离两个组件来解决此问题:

var urlComponents = URLComponents(string: "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=24.712587,46.673184&radius=5000&key=xxx")!
urlComponents.queryItems?.append(URLQueryItem(name: "name", value: "ستاربكس"))
let requestURL =
    Alamofire.request(urlComponents).responseJSON { response in
        ...
    }

最新更新