Source of file GoogleGeocoding.php
Size: 1,071 Bytes - Last Modified: 2021-12-24T05:16:09+00:00
/var/www/docs.ssmods.com/process/src/code/GoogleGeocoding.php
1234567891011121314151617181920212223242526272829303132333435363738394041 | <?php /** * A utility class for geocoding addresses using the google maps API. * * @package silverstripe-addressable */ class GoogleGeocoding { /** * Convert an address into a latitude and longitude. * * @param string $address The address to geocode. * @param string $region An optional two letter region code. * @return array An associative array with lat and lng keys. */ public static function address_to_point($address, $region = null) { // Get the URL for the Google API $url = Config::inst()->get('GoogleGeocoding', 'google_api_url'); $key = Config::inst()->get('GoogleGeocoding', 'google_api_key'); // Query the Google API $service = new RestfulService($url); $service->setQueryString(array( 'address' => $address, 'sensor' => 'false', 'region' => $region )); $response = $service->request()->simpleXML(); if ($response->status != 'OK') { return false; } $location = $response->result->geometry->location; return array( 'lat' => (float) $location->lat, 'lng' => (float) $location->lng ); } } |