用swift在谷歌地图上显示坐标

时间:2022-10-28 22:45:55

I am getting the co-ordinates from my json api

我从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

        }
    }

but I am unable to show it on the google maps. Honestly I don't know how can I show my co-ordinates on google maps?. Any Help?

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

用swift在谷歌地图上显示坐标 //getting co-ordinates in postman

/ /获取坐标的邮差

用swift在谷歌地图上显示坐标

1 个解决方案

#1


1  

You need to convert your array of strings coordinates in CLLocationCoordinate2D coordinates and create both markers and assign them to your GMSMapView

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

Code

代码

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

        }
    }

#1


1  

You need to convert your array of strings coordinates in CLLocationCoordinate2D coordinates and create both markers and assign them to your GMSMapView

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

Code

代码

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

        }
    }