首页 > 解决方案 > 带有谷歌地图 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`AgApAaC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hBaEfAsTvEmEr@iCr@qDrAiFnCcEzCaE~D_@JmFdGQDwBvCeErEoD|BcFjC}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"
    }
]

标签: iosjsonswiftgoogle-mapsmaps

解决方案


错误消息表明您没有在请求中提供您的 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"

推荐阅读