Parsing Google map distance JSON

  1. 10 months ago

    How to Parsing json from Google map distance matrix responce.

    Here is my code to parse
    let url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=\(parameters["cur_latitude"]!),\(parameters["cur_longitude"]!)&destinations=\(parameters["des_latitude"]!),\(parameters["des_longitude"]!)&key=\(parameters["mapAPIKEy"]!)"

    print("All service list URL===== \(url)");

    Alamofire.request(.GET, url, parameters: nil,
    headers: postheaders)
    .response { request, response, data, error in

    let myData = try NSJSONSerialization.JSONObjectWithData(data!,
    options: NSJSONReadingOptions.MutableContainers) as! NSDictionary

    print("All service listWS ===== \(myData)")
    successCallback(result: myData)


    }

    and on "successCallback" i am getting the below responce

    "destination_addresses" = (
    "Unnamed Road - Sharjah - United Arab Emirates"
    );
    "origin_addresses" = (
    "Unnamed Road - Dubai - United Arab Emirates"
    );
    rows = (
    {
    elements = (
    {
    distance = {
    text = "42.7 mi";
    value = 68744;
    };
    duration = {
    text = "49 mins";
    value = 2947;
    };
    status = OK;
    }
    );
    }
    );
    status = OK;
    }

    How will i parse and get the distance text from this json ? that is rows-> elements -> distance-> text

    I tried this but no luck

    successCallback (result : AnyObject){

    print("navigation window\(result["rows"])") // this one prints data inside "elements = " tags

    let twDataArray = (result as! NSDictionary) as Dictionary

    print("navigation window\(twDataArray["rows"]?["elements"])") // Nothing is printing here

or Sign Up to reply!