Andrew G. Johnson
Andrew G. Johnson

Reputation: 26993

Is there a way to get long/lat for a given address in Google Maps with only PHP?

I know this is possible in Javascript but I need to do it using only PHP, how can I?

Upvotes: 12

Views: 12266

Answers (5)

Sebastian Viereck
Sebastian Viereck

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

Amit Sanghani
Amit Sanghani

Reputation: 1

You can use a Google Maps API to get address from lat and long (example usage).

Upvotes: 0

Tizón
Tizón

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

mauris
mauris

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

Mike Williams
Mike Williams

Reputation: 7749

Yes, there's a HTTP geocoding interface that you can call from any server language.

Upvotes: 13

Related Questions