wing suet cheung
wing suet cheung

Reputation: 205

Make a request for The Google Maps Geolocation API

I would like to make a request for the web service - Google Geolocation API by using PHP 5. I have experience in XML parsing using PHP, but this one is using JSON format for making the request. I have no idea how to make the request! I think to receive the request is similar the XML parsing!

https://developers.google.com/maps/documentation/business/geolocation/

Upvotes: 0

Views: 13753

Answers (3)

GerritElbrink
GerritElbrink

Reputation: 352

To add to mohamed's answer:

function getGeoPosition($address){
    $url = "https://maps.google.com/maps/api/geocode/json?sensor=false" .
        "&key=YOUR_API_KEY_HERE&address=" . urlencode($address);

    $json = file_get_contents($url);

    $data = json_decode($json, TRUE);
     if($data['status']=="OK"){
      return $data['results'];
    }
}
print_r(getGeoPosition('chicago,il'));

Note that the request needs to be made over https and needs an API key which is not restricted.

Upvotes: 0

mohamed elbou
mohamed elbou

Reputation: 1857

try this(result in json format):

public function getGeoPosition($address){
    $url = "http://maps.google.com/maps/api/geocode/json?sensor=false" . 
        "&address=" . urlencode($address);

    $json = file_get_contents($url);

    $data = json_decode($json, TRUE);

     if($data['status']=="OK"){
      return $data['results'];
    }

}

print_r(getGeoPosition('chicago,il'));

Upvotes: 7

david strachan
david strachan

Reputation: 7228

If you have a Business account ,as suggested by your link, Google supply help. If you don't have a business account you would will need to use use This API which provides xml results. ie

 http://maps.googleapis.com/maps/api/geocode/xml?address=anywhere&sensor=true

or

 sensor=false

Upvotes: 0

Related Questions