I’m trying to use google maps coordinates for use in an XML feed for a third party site, which should create the map itself. Im using the URL http://maps.google.com/maps/geo?q=hong%20kong&output=json&oe=utf8&sensor=false (clicking this link won’t work, but copying and pasting does). This gives me the point coordinates 114.1094970 and 22.3964280. If I put these coordinates straight into google maps they don’t work, so what am I doing wrong?
My code for the function itself is as follows, but it seems to be an inherent problem with google maps rather than my code:
function getLatandLong($add)
{
$url = 'http://maps.google.com/maps/geo?q='.urlencode($add).'&output=json&oe=utf8&sensor=false';
$data = @file_get_contents($url);
$jsondata = json_decode($data,true);
if(is_array($jsondata )&& $jsondata ['Status']['code'] == 200){
$output['lat'] = $jsondata ['Placemark'][0]['Point']['coordinates'][0];
$output['lng'] = $jsondata ['Placemark'][0]['Point']['coordinates'][1];
}
return $output;
}
Any help would be much appreciated!
These coordinates are correct and work in Google Maps.
I’m guessing this is because you pass them in the wrong order:
you need to put the latitude coordinate first, as in
22.396428 114.109497not
114.109497 22.396428Indeed in looking at the code snippet, your parse them incorrectly: the JSON data has the longitude first and the latitude second… Code should therefore read: