...

View Full Version : Extracting lat and long from google map json



dk4210
01-13-2012, 11:56 AM
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

XmisterIS
01-13-2012, 12:31 PM
I'm not familiar with the Google Maps API,

but it looks to me as if in the general case, the lat and long would be extracted by:



$geocode=file_get_contents('http://maps.google.com/maps/geo?output=json&q=795PollardBlvdSW,Atlanta,GA');

$output= json_decode($geocode);

//Extract the placemark data into two arrays $long and $lat.
$long = $lat = array();

foreach ($output->Placemark as $i => $placemark)
list($long[$i], $lat[$i]) = $placemark->Point->coordinates;

var_dump($lat);
var_dump($long);


The first number is long, then lat because 33 deg. North, 84 deg. West sounds about right for a city somewhere in the United States. (I have no idea where Atlanta actually is though, I've never been to the USA!)

dk4210
01-13-2012, 12:53 PM
I just got the fix and it is I had this wrong

$lat = $output->Placemark[0]->Point->coordinates[1];
$long = $output->Placemark[0]->Point->coordinates[0];


It's working fine now! Thanks for your help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum