dk4210 Posted January 13, 2012 Share Posted January 13, 2012 Hello Guys, I need some help here.. I am trying to extract the lat and long from a json query from google. Here is what I receive back from json { "name": "795PollardBlvdSW,Atlanta,GA", "Status": { "code": 200, "request": "geocode" }, "Placemark": [ { "id": "p1", "address": "Atlanta, GA, USA", "AddressDetails": { "Accuracy" : 4, "Country" : { "AdministrativeArea" : { "AdministrativeAreaName" : "GA", "SubAdministrativeArea" : { "Locality" : { "LocalityName" : "Atlanta" }, "SubAdministrativeAreaName" : "Fulton" } }, "CountryName" : "USA", "CountryNameCode" : "US" } }, "ExtendedData": { "LatLonBox": { "north": 33.8231844, "south": 33.6747422, "east": -84.2599230, "west": -84.5160418 } }, "Point": { "coordinates": [ -84.3879824, 33.7489954, 0 ] } } ] } Here is the code I am using $geocode=file_get_contents('http://maps.google.com/maps/geo?output=json&q=795PollardBlvdSW,Atlanta,GA'); $output= json_decode($geocode); $lat = $output->results[0]->geometry->location->lat; $long = $output->results[0]->geometry->location->lng; echo $lat; echo $long; Just not sure how to pull the lat and long out of a json into vars.. Please advise.. Dan Quote Link to comment https://forums.phpfreaks.com/topic/254936-extracting-lat-and-long-from-google-map-json/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 13, 2012 Share Posted January 13, 2012 If you use the following debugging statement in your code, it will be clearer how you would reference the values you want - echo "<pre>",print_r($output,true),"</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/254936-extracting-lat-and-long-from-google-map-json/#findComment-1307167 Share on other sites More sharing options...
silkfire Posted January 13, 2012 Share Posted January 13, 2012 You're accessing the JSON object in the wrong way. Change it to: $lat = $output->Placemark[0]->Point->coordinates[1]; $long = $output->Placemark[0]->Point->coordinates[0]; Quote Link to comment https://forums.phpfreaks.com/topic/254936-extracting-lat-and-long-from-google-map-json/#findComment-1307169 Share on other sites More sharing options...
dk4210 Posted January 13, 2012 Author Share Posted January 13, 2012 PERFECT - silkfire! worked like a champ! Thnak you also PFMaBiSmAd ! Quote Link to comment https://forums.phpfreaks.com/topic/254936-extracting-lat-and-long-from-google-map-json/#findComment-1307179 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.