Reputation: 324
I'm trying to pave the way on google maps. Wrote a function and json file. I think that I made a mistake from the coordinates, or by json itself. Please help me find the error in the code. The documentation from Google is a little incomprehensible to me, so I can’t completely get it right.
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
}
}
}
}
routes.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{YlZiBdBaIhGkFrDeCtBuFxFmIdJmOjPaChDeBlDiAdD}ApGcDxU}@hEmAxD}[t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wr@}@vAgCbEaHfMqA`Cy@dAg@bAO`@gCi@w@W"
},
{
"summary" : "I-5 N and US-101 N",
"warnings" : [],
"waypoint_order" : []
"status" : "OK"
}
]
Upvotes: 0
Views: 505
Reputation: 414
The error message indicates that you have not provided your API key in the request. From the beginning of the response...
"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"
Upvotes: 1