ios - 带有谷歌地图 API 路线的 Json 文件
问题描述
我正试图在谷歌地图上铺平道路。写了一个函数和json文件。我认为我从坐标或json本身犯了一个错误。请帮我找出代码中的错误。谷歌的文档对我来说有点难以理解,所以我不能完全正确。
class MapView: UIView,CLLocationManagerDelegate,GMSMapViewDelegate {
@IBOutlet var contentView: UIView!
private func initView() {
Bundle.main.loadNibNamed("MapView", owner: self, options: nil)
contentView = mapView
addSubview(contentView)
contentView.frame = self.bounds
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startMonitoringSignificantLocationChanges()
mapView.delegate = self
mapView.isMyLocationEnabled = true
self.drawPath()
}
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: GMSCameraPosition.camera(withLatitude: 49, longitude: 98, zoom: 15.0))
func drawPath()
{
let origin = "\(49),\(98)"
let destination = "\(49.3),\(96)"
let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving&key=MY_KEY"
Alamofire.request(url).responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
let json = try? JSON(data: response.data!)
let routes = json!["routes"].arrayValue
for route in routes
{
let routeOverviewPolyline = route["overview_polyline"].dictionary
let points = routeOverviewPolyline?["points"]?.stringValue
let path = GMSPath.init(fromEncodedPath: points!)
let polyline = GMSPolyline.init(path: path)
polyline.map = self.mapView
}
}
}
}
路线.json
[
{
"error_message" : "You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to http://g.co/dev/maps-no-account",
"routes" : [],
"status" : "REQUEST_DENIED"
},
"overview_polyline" : {
"points" : "knjmEnjunUbKCfEA?_@]@kMBeE@qIIoF@wH@eFFk@WOUI_@?u@j@k@`@EXLTZHh@Y`AgApAaCrCUd@cDpDuAtAoApA{YlZiBdBaIhGkFrDeCtBuFxFmIdJmOjPaChDeBl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cFjC}DbEuD~D`@Zr@h@?d@Wr@}@vAgCbEaHfMqA`Cy@dAg@bAO`@gCi@w@W"
},
{
"summary" : "I-5 N and US-101 N",
"warnings" : [],
"waypoint_order" : []
"status" : "OK"
}
]
解决方案
错误消息表明您没有在请求中提供您的 API 密钥。从回复开始...
"error_message" : "You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to http://g.co/dev/maps-no-account"
推荐阅读
- python - 从python代码中获取列表索引超出范围
- apache-kafka - 本地单主机Fabric网络中基于kafka的OSN,但是当我执行事务时没有登录kafka容器
- javascript - 如何使滑块的长度更短并出现在页面的中心?
- json - 如何将循环集成到我的代码中,将我的请求应用于每个 filterViewId?
- python - 为一个接口创建多个 IP,并能够在特定端口上的每个 IP 上运行服务
- linux - Linux 父/子进程组和信号
- arrays - 在 Neo4j 中使用 apoc.load.jason() 时如何拥有一组 Json 键对象?
- symfony - 如何防止实体在容器中被列为服务?
- arrays - 按组复制 SAS 中的值
- c++ - 嵌套别名模板的模板参数推导编译器差异