Reputation: 26993
I know this is possible in Javascript but I need to do it using only PHP, how can I?
Upvotes: 12
Views: 12266
Reputation: 5885
here is my class:
class GeoCoder {
public static function getLatLng($address){
try{
$address = urlencode($address);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$address.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$lng = $output->results[0]->geometry->location->lng;
if(is_numeric($lat) && is_numeric($lng)){
return array("lat" => $lat, "lng" => $lng);
}
}
catch(Exception $e){
return false;
}
}
}
Upvotes: 3
Reputation: 1
You can use a Google Maps API to get address from lat and long (example usage).
Upvotes: 0
Reputation: 91
And other way:
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$address.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
Upvotes: 8
Reputation: 43619
Using Google Map API and PHP to get coordinates:
$url = 'http://maps.google.com/maps/geo?q='.$address.'&output=json&oe=utf8&sensor=false&key='.$api_key;
$data = @file_get_contents($url);
$jsondata = json_decode($data,true);
if(is_array($jsondata )&& $jsondata ['Status']['code']==200){
$lat = $jsondata ['Placemark'][0]['Point']['coordinates'][0];
$lon = $jsondata ['Placemark'][0]['Point']['coordinates'][1];
}
Upvotes: 4
Reputation: 7749
Yes, there's a HTTP geocoding interface that you can call from any server language.
Upvotes: 13