I am Creating a webapp where i could get the distance and time of travel using google map API..
https://maps.googleapis.com/maps/api/distancematrix/json?origins=10.964,76.007&destinations=10.982,75.999
I am using HTTP method as given by google..
Its returning JSON Format as give below
{
"destination_addresses" : [ "Kanyakumari - Panvel Highway, Palathara, Kerala 676501, India" ],
"origin_addresses" : [ "Kanyakumari - Panvel Highway, Randathani, Kerala 676510, India" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "2.5 km",
"value" : 2526
},
"duration" : {
"text" : "4 mins",
"value" : 228
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
How could i extract the distance text and duration text using jquery and alert it..
Use file_get_contents() and json_decode() the output..
$contents = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?origins=10.964,76.007&destinations=10.982,75.999');
$data = json_decode($contents,true);
echo 'Distance : '.$data['rows'][0]['elements'][0]['distance']['text'];
echo "<br>";
echo 'Duration : '.$data['rows'][0]['elements'][0]['duration']['text'];
This will give you :
Distance : 2.5 km
Duration : 4 mins
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I have to try to access the value of "text" which is 4.9 in the JSON data below in PHP.
$json = '{"destination_addresses" : [ "Ibadan-Abeokuta Rd, Mokola, Ibadan, Nigeria" ],
"origin_addresses" : [ "Ogunwale Crescent, Ibadan, Nigeria" ],
"rows" : [
{ "elements" : [
{ "distance" :
{ "text" : "4.9 mi", "value" : 7819 },
"duration" : { "text" : "18 mins", "value" : 1067 },
"status" : "OK" }
]
}
]
, "status" : "OK" }';
Your JSON is a little bit complex. I have a solution below that access text property from the nested JSON.
$array = json_decode( $json, true );
echo ($array['rows'][0] ["elements"][0]['distance']['text']);
How to print the Results from Google Distance matrix iam Getting a results like this:
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "4,8 km",
"value" : 4820
},
"duration" : {
"text" : "17 minutes",
"value" : 1038
},
"status" : "OK"
},
{
"distance" : {
"text" : "11,1 km",
"value" : 11064
},
"duration" : {
"text" : "25 minutes",
"value" : 1506
},
"status" : "OK"
}
]
}
],
And My Php Code to print The Output is
$json = file_get_contents($url);
$result = json_decode($json, true);
for($i=0; $i<count($result['rows']); $i++) {
echo "Kilometers " . $result['rows'][$i]["elements"][$i]['distance']['text'];
}
.Please Help
Given that you haven't included all the response, the method invlolved in the retrieval of the data nor the final desired output it is tricky to answer but I hope the following will give guidance on how to render the output.
This uses example data from the interwebs rather than the partial example code given in the question - a bit rough perhaps but...
$res='{
"destination_addresses" : [
"Commons Way, Bridgewater, NJ 08807, USA",
"Morris Turnpike, Short Hills, NJ 07078, USA",
"Monmouth Mall, Eatontown, NJ 07724, USA",
"Garden State Plaza Blvd, Paramus, NJ 07652, USA",
"Newport Centre Mall, Jersey City, NJ 07302, USA"
],
"origin_addresses" : [ "75 Ninth Ave, New York, NY 10011, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "68.8 km",
"value" : 68781
},
"duration" : {
"text" : "56 mins",
"value" : 3334
},
"duration_in_traffic" : {
"text" : "1 hour 1 min",
"value" : 3687
},
"status" : "OK"
},
{
"distance" : {
"text" : "34.8 km",
"value" : 34806
},
"duration" : {
"text" : "36 mins",
"value" : 2138
},
"duration_in_traffic" : {
"text" : "41 mins",
"value" : 2487
},
"status" : "OK"
},
{
"distance" : {
"text" : "86.3 km",
"value" : 86322
},
"duration" : {
"text" : "1 hour 6 mins",
"value" : 3930
},
"duration_in_traffic" : {
"text" : "1 hour 7 mins",
"value" : 4044
},
"status" : "OK"
},
{
"distance" : {
"text" : "32.3 km",
"value" : 32278
},
"duration" : {
"text" : "33 mins",
"value" : 2009
},
"duration_in_traffic" : {
"text" : "35 mins",
"value" : 2082
},
"status" : "OK"
},
{
"distance" : {
"text" : "6.9 km",
"value" : 6879
},
"duration" : {
"text" : "19 mins",
"value" : 1113
},
"duration_in_traffic" : {
"text" : "24 mins",
"value" : 1444
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}';
/* return as an object for easier notation */
$json=json_decode( $res, false );
$origins = $json->origin_addresses;
$destinations=$json->destination_addresses;
/* if there are multiple rows, use a loop and `$rows[$i]` etc */
$elements=$json->rows[0]->elements;
foreach( $elements as $i => $obj ){
echo '
<div>
<h5>From: '.$origins[ 0 ].' to '.$destinations[ $i ].'</h5>
Distance: '.$obj->distance->text.',
Duration:'.$obj->duration->text.',
In traffic: '.$obj->duration_in_traffic->text.',
Status: '.$obj->status.'
</div>';
}
Here is an example using a nested foreach, this is not the only way to render the output but it works.
$json = file_get_contents($url);
$result = json_decode($json, true);
foreach ($result['rows'][0] as $elements => $element) {
foreach ($element as $key => $value) {
echo $value['distance']['text'];
echo $value['duration']['text'];
echo $value['status'];
echo '<br>';
}
}
All we are doing is accessing a multi-dimensional array using $key => $value and then $value[$key] notation... I would suggest a crash course on how to handle arrays in php, this one is great: https://www.youtube.com/watch?v=57y5maglayQ
You can also look at the overall structure of your array by writing echo '<pre>', var_dump($result), '</pre>;' which will show your array => key pairs.
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
I have this response from Google Maps Distance Matrix API:
{
"destination_addresses" : [ "334-350 Hicks St, Brooklyn, NY 11201, USA" ],
"origin_addresses" : [ "565-569 Vermont St, Brooklyn, NY 11207, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "6.5 mi",
"value" : 10410
},
"duration" : {
"text" : "34 mins",
"value" : 2045
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
How to exactly get the "6.5 mi" value?
I tried this but doesn't work:
$distance = $response_a['rows'][0]['elements'][0]['distance']['text'];
You received a JSON string from Google API. Decode it before using.
$arr = json_decode($response_a, true);
echo $arr['rows'][0]['elements'][0]['distance']['text'];
I am using the file_get_contents() function in PHP to retrieve the contents on a page containing a JSON response. However, I am struggling to get the "text" data.
$response = file_get_contents(someData)
I can use var_dump(json_decode($response)); to show the data, however, I am trying to get the data from the "text" field only within duration.
So far I have tried
$response[0];
$response->rows[0]->elements[0]->duration[0]->text;
But I cannot seem to get the data. I have pasted the response below
{
"destination_addresses" : [ "Manchester, UK", "Liverpool, Merseyside, UK" ],
"origin_addresses" : [ "London, UK" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "335 km",
"value" : 335444
},
"duration" : {
"text" : "3 hours 36 mins",
"value" : 12955
},
"status" : "OK"
},
{
"distance" : {
"text" : "354 km",
"value" : 354415
},
"duration" : {
"text" : "3 hours 43 mins",
"value" : 13387
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
$response = json_decode(file_get_contents(someData));
$text = $response->rows[0]->elements[0]->duration->text;
var_dump($text);
Your only mistake was a [0] after duration.
i am making a android application n use a .php file to call java script file that returns me a JSON output, now my problem is the output is in a valid JSON format, but i am confused as to how to parse the values. the output is
{
"destination_addresses" : [
"Kambarganvi road, Durgadakeri, Karnataka 580011, India",
"St Inez Road, Santa Inez, Panjim, Goa 403001, India",
"Dr Braganza Pereira Road, Santa Inez, Panjim, Goa 403001, India",
"Dayanand Bandodkar Marg, Santa Inez, Panjim, Goa 403001, India",
"St Inez Road, Santa Inez, Panjim, Goa 403001, India",
"18th June Road, Santa Inez, Panjim, Goa 403001, India"
],
"origin_addresses" : [ "Suyog Residency, St Joaquim Road, Borda, Margao, Goa 403602, India" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "133 km",
"value" : 132717
},
"duration" : {
"text" : "1 day 4 hours",
"value" : 100199
},
"status" : "OK"
},
{
"distance" : {
"text" : "33.4 km",
"value" : 33427
},
"duration" : {
"text" : "6 hours 54 mins",
"value" : 24832
},
"status" : "OK"
},
{
"distance" : {
"text" : "33.5 km",
"value" : 33518
},
"duration" : {
"text" : "6 hours 55 mins",
"value" : 24923
},
"status" : "OK"
},
{
"distance" : {
"text" : "33.8 km",
"value" : 33834
},
"duration" : {
"text" : "7 hours 0 mins",
"value" : 25170
},
"status" : "OK"
},
{
"distance" : {
"text" : "33.5 km",
"value" : 33503
},
"duration" : {
"text" : "6 hours 55 mins",
"value" : 24893
},
"status" : "OK"
},
{
"distance" : {
"text" : "33.5 km",
"value" : 33457
},
"duration" : {
"text" : "6 hours 55 mins",
"value" : 24880
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
i want to parse the values in such a way that i can place the distances of the result should be put in a data structure like an array and then sort them.
Thank you in advance.
use this to make own model .
http://www.jsonschema2pojo.org/
after making model you can use bwlow code.
Gson gson = new Gson();
gson.fromJson(jData, AddPostModel.class);
AddPostModel have all the data which comes from server.