Google Maps iOS SDK, Getting Directions between 2 locations

前端 未结 11 1314
栀梦
栀梦 2020-12-02 07:52

While I am using Google Maps SDK, I am trying to get driving direction between two locations on iOS. I know we can do this using two methods:-

1.) Using URL Scheme,

11条回答
  •  执念已碎
    2020-12-02 08:43

    Swift 3.0 & XCode 8.0 Using AFNetworking & SwiftJson

            let destLatitude="26.9124"
            let destLongitude="75.7873"
            mapView.isMyLocationEnabled = true
            var urlString = "\("https://maps.googleapis.com/maps/api/directions/json")?origin=\("28.7041"),\("77.1025")&destination=\(destLatitude),\(destLongitude)&sensor=true&key=\("Your-Api-key")"
    
            urlString = urlString.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)!
    
            let manager=AFHTTPRequestOperationManager()
    
            manager.responseSerializer = AFJSONResponseSerializer(readingOptions: JSONSerialization.ReadingOptions.allowFragments) as AFJSONResponseSerializer
    
            manager.requestSerializer = AFJSONRequestSerializer() as AFJSONRequestSerializer
    
            manager.responseSerializer.acceptableContentTypes = NSSet(objects:"application/json", "text/html", "text/plain", "text/json", "text/javascript", "audio/wav") as Set
    
    
            manager.post(urlString, parameters: nil, constructingBodyWith: { (formdata:AFMultipartFormData!) -> Void in
    
                }, success: {  operation, response -> Void in
                    //{"responseString" : "Success","result" : {"userId" : "4"},"errorCode" : 1}
                    //if(response != nil){
                    let parsedData = JSON(response)
                    print_debug("parsedData : \(parsedData)")
                   var path = GMSPath.init(fromEncodedPath: parsedData["routes"][0]["overview_polyline"]["points"].string!)
                     //GMSPath.fromEncodedPath(parsedData["routes"][0]["overview_polyline"]["points"].string!)
                    var singleLine = GMSPolyline.init(path: path)
                    singleLine.strokeWidth = 7
                    singleLine.strokeColor = UIColor.green
                    singleLine.map = self.mapView
                    //let loginResponeObj=LoginRespone.init(fromJson: parsedData)
    
    
                    //  }
                }, failure: {  operation, error -> Void in
    
                    print_debug(error)
                    let errorDict = NSMutableDictionary()
                    errorDict.setObject(ErrorCodes.errorCodeFailed.rawValue, forKey: ServiceKeys.keyErrorCode.rawValue as NSCopying)
                    errorDict.setObject(ErrorMessages.errorTryAgain.rawValue, forKey: ServiceKeys.keyErrorMessage.rawValue as NSCopying)
    
            })
    

提交回复
热议问题