PHP implementation for Google Geocoder API

/** * Class to get the geolocation of a given adress */ class GeocoderService { private $url = "http://maps.google.com/maps/api/geocode/json?address="; private $sensor = "&sensor=false"; private $region = "&region=Europe"; /** * * @param type $address the address * @return type object with lat ang lng of the given location */ public function getLocation($address) { $location = urlencode($address); $url = $this->url.$location.$this->sensor.$this->region; $cURL = curl_init(); curl_setopt($cURL, CURLOPT_URL, $url); curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1); curl_setopt($cURL, CURLOPT_PROXYPORT, 3128); curl_setopt($cURL, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, 0); $result = curl_exec($cURL); curl_close($cURL); $result_a = json_decode($result); // Get lat and lng $data['lat'] = $result_a->results[0]->geometry->location->lat; $data['lng'] = $result_a->results[0]->geometry->location->lng; return $data; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.