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

后端 未结 2 1414
清酒与你
清酒与你 2020-12-23 17:31

I\'m fairly new to JSON, and I\'m trying to get the latitude and longitude of a geocoded city from the Google Maps API using curl. The function I\'m using is:



        
2条回答
  •  旧时难觅i
    2020-12-23 18:11

    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.

提交回复
热议问题