Search code examples
iosswiftgoogle-mapscllocationcoordinate2d

Show co-ordinates on google map in swift


I am getting the co-ordinates from my 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?

enter image description here //getting co-ordinates in postman

enter image description here


Solution

  • You need to convert your array of strings coordinates in CLLocationCoordinate2D coordinates and create both markers and assign them to your 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
    
            }
        }