在swift

时间:2018-05-02 13:27:27

标签: ios swift google-maps cllocationcoordinate2d

我从json api获得坐标

Alamofire.request(Constants.API.url("api/trip/"mykey"/startTrip"), method: .post, parameters: nil, encoding: URLEncoding.default, headers: header).responseJSON { (response:DataResponse<Any>) in
        self.progressHUD.dismiss(afterDelay: 0.5)
        switch(response.result) {
        case .success(_):
            guard let json = response.result.value as! [String:Any]? else{ return}
            guard let response = json["response"] as! [String:Any]? else{ return}
            guard let endCoordinates = response["end_coordinates"] as? [CLLocationDegrees] else { return }
            print(endCoordinates)
            guard let startCoordinates = response["start_coordinates"] as? [CLLocationDegrees] else { return}
            print(startCoordinates)

            let markerStart = GMSMarker(position: CLLocationCoordinate2D(latitude: startCoordinates[0] , longitude: startCoordinates[1] ))
            print(markerStart)
            markerStart.map = self.mapView

            let markerEnd = GMSMarker(position: CLLocationCoordinate2D(latitude: endCoordinates[0] , longitude: endCoordinates[1] ))
            markerEnd.map = self.mapView
            print(markerEnd)
            break

        case .failure(_):
            print("error")
            break

        }
    }

但我无法在谷歌地图上显示它。老实说,我不知道如何在谷歌地图上显示我的坐标?任何帮助?

enter image description here //在邮递员中获得坐标

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要转换CLLocationCoordinate2D坐标中的字符串坐标数组并创建两个标记并将其分配给GMSMapView

<强>代码

Alamofire.request(Constants.API.url("api/trip//startTrip"), method: .post, parameters: nil, encoding: URLEncoding.default, headers: header).responseJSON { (response:DataResponse<Any>) in
        self.progressHUD.dismiss(afterDelay: 0.5)
        switch(response.result) {
        case .success(_):
            guard let json = response.result.value as? [String:Any] else{ return}
            guard let response = json["response"] as? [String:Any] else{ return}
            guard let endCoordinates = response["end_coordinates"] as? [CLLocationDegrees] else { return }
            print(endCoordinates)
            guard let startCoordinates = response["start_coordinates"] as? [CLLocationDegrees] else { return}
            print(startCoordinates)

            DispatchQueue.main.async {
                let markerStart = GMSMarker(position: CLLocationCoordinate2D(latitude: startCoordinates[0], longitude: startCoordinates[1])) 
                markerStart.map = self.mapView 

                let markerEnd = GMSMarker(position: CLLocationCoordinate2D(latitude: endCoordinates[0], longitude: endCoordinates[1])) 
                markerEnd.map = self.mapView 
             }

        case .failure(_):
            print("error")
            break

        }
    }