I just need the coordinates from the geolocation based on the address i put in the params. I am using curl for the request.
Here is my curl request
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=10%20BISHAN%STREET%2013&key='.$key; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = json_decode(curl_exec($ch), true); dd($response);
I got a response of
array:2 [▼ "results" => array:1 [▼ 0 => array:6 [▼ "address_components" => array:6 [▶] "formatted_address" => "10 Bishan Street 13, Singapore 579795" "geometry" => array:3 [▼ "location" => array:2 [▼ "lat" => 1.349774 "lng" => 103.854975 ] "location_type" => "ROOFTOP" "viewport" => array:2 [▶] ] "place_id" => "ChIJR0tGixEX2jERVE4uZWfBN-o" "plus_code" => array:2 [▶] "types" => array:1 [▶] ] ] "status" => "OK" ]
If i do dd($response['result']);
I get
0 => array:6 [▼ "address_components" => array:6 [▶] "formatted_address" => "10 Bishan Street 13, Singapore 579795" "geometry" => array:3 [▼ "location" => array:2 [▼ "lat" => 1.349774 "lng" => 103.854975 ] "location_type" => "ROOFTOP" "viewport" => array:2 [▼ "northeast" => array:2 [▼ "lat" => 1.3511229802915 "lng" => 103.85632398029 ] "southwest" => array:2 [▼ "lat" => 1.3484250197085 "lng" => 103.85362601971 ] ] ] "place_id" => "ChIJR0tGixEX2jERVE4uZWfBN-o" "plus_code" => array:2 [▼ "compound_code" => "8VX3+WX Singapore" "global_code" => "6PH58VX3+WX" ] "types" => array:1 [▼ 0 => "street_address" ] ] ]
Is there a way i can access location
directly using dd
?. Thanks for the answers.
Advertisement
Answer
If you follow the hierarchy, you just add the corresponding array element name at each stage, this should give you…
$response['result'][0]['geometry']['location']
The [0]
may need a foreach
if there are more results.