Using PHP (curl) to pull data from JSON (Google Maps API)

荒凉一梦 提交于 2019-11-29 21:34:16

Note that it seems that results contains an array (with only one item in it, here) of results ; and geometry is one item inside one result.

Here, you can see that results' content is delimited by [] -- which indicates it's an array.


So, you have to first access the first result : $geoloc['results'][0]
Inside of which you'll have the geometry : $geoloc['results'][0]['geometry']

Which will allow you to get the latitude and longitude :

var_dump($geoloc['results'][0]['geometry']['location']['lat']);
var_dump($geoloc['results'][0]['geometry']['location']['lng']);


I suppose that, depending on the address you've searched on, you will sometimes have more than one item in the results array.

Fernando Cardenas 'Pelonxz'

You just need this :

$fullurl = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true";
$string .= file_get_contents($fullurl); // get json content
$json_a = json_decode($string, true); //json decoder

echo $json_a['results'][0]['geometry']['location']['lat']; // get lat for json
echo $json_a['results'][0]['geometry']['location']['lng']; // get ing for json
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!