-
-
Save thomaslarsson/5938143 to your computer and use it in GitHub Desktop.
Simple google maps helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple Google maps address lookup | |
* | |
* Enables you to get latitude and longitude for an address | |
* | |
* @author Thomas Maurstad Larsson, Logoz As <thomas@logoz.no> | |
* @link http://logoz.no | |
* @copyright Copyright (c) 2012 | |
*/ | |
class SimpleGoogleMaps | |
{ | |
/** @var String The main URL to the Google Maps API */ | |
const GOOGLE_MAPS_BASE_URL = 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false'; | |
/** @var String A String constant used by Google when no results are found */ | |
const GOOGLE_CONST_ZERO_RESULTS = 'ZERO_RESULTS'; | |
/** @var String The get-param to search by */ | |
const GOOGLE_MAPS_SEARCH_BY = 'address'; | |
/** @var String Delimiter used in Strings */ | |
const URL_REQUEST_DELIMITER = "+"; | |
/** @var String An error message used by thrown Exceptions when an adress is not found */ | |
const NO_ADDRESS_FOUND = 'Could not find the address.'; | |
/** @var String The URL for the request */ | |
private $requestUrl = ""; | |
/** @var Array The result array following a request */ | |
private $result; | |
private $address = ''; | |
/** | |
* Public constructor | |
* | |
* Will build the request string for the new requests following class constants | |
*/ | |
public function __construct() | |
{ | |
$this->requestUrl = self::GOOGLE_MAPS_BASE_URL.'&'.self::GOOGLE_MAPS_SEARCH_BY.'='; | |
} | |
/** | |
* Will look for an address provided as a simple String. | |
* The format of this String is pretty flexible. | |
* | |
* @param $address The address to look for | |
* @return boolean True if found, else false | |
* | |
* @see The Google Maps API | |
*/ | |
public function find( $address ) | |
{ | |
// Save address | |
$this->address = $address; | |
// Convert to an ascii, non-whitespace string | |
$address = $this->toLowerCaseAsciiDelimitedString($address, self::URL_REQUEST_DELIMITER); | |
// Read results from the Google API | |
$this->result = json_decode(file_get_contents($this->requestUrl.$address), TRUE); | |
// Make sure a result is found | |
return ( $this->result !== false ); | |
} | |
/** | |
* Will check if one or more addresses are found | |
* | |
* @return boolean True if one was found, else false | |
*/ | |
public function resultExists() | |
{ | |
return $this->result['status'] != self::GOOGLE_CONST_ZERO_RESULTS && count($this->result['results']) > 0; | |
} | |
/** | |
* Will check if multiple possible locations are found | |
* | |
* @return boolean True if several results, else false | |
*/ | |
public function hasMultipleResults() | |
{ | |
return count($this->result['results']) > 1; | |
} | |
/** | |
* Returns the address searched for as a String | |
* | |
* @return String The address searched for | |
* @throws AddressNotFoundException xception if no address is found | |
*/ | |
public function getAddress() | |
{ | |
if ( ! $this->resultExists() ) | |
{ | |
throw new AddressNotFoundException(self::NO_ADDRESS_FOUND); | |
} | |
return $this->address; | |
} | |
/** | |
* Returns the latitude for the address if one is found | |
* | |
* @return double The latitude as a decimal point number | |
* @throws AddressNotFoundException Exception if no address is found | |
*/ | |
public function getLatitude() | |
{ | |
// Make sure we have an address | |
if ( ! $this->resultExists() ) | |
{ | |
throw new AddressNotFoundException(self::NO_ADDRESS_FOUND); | |
} | |
return $this->result['results'][0]['geometry']['location']['lat']; | |
} | |
/** | |
* Returns the longitude for the address if one is found | |
* | |
* @return double The longitude as a decimal point number | |
* @throws AddressNotFoundException Exception if no address is found | |
*/ | |
public function getLongitude() | |
{ | |
// Make sure we have an address | |
if ( ! $this->resultExists() ) | |
{ | |
throw new AddressNotFoundException(self::NO_ADDRESS_FOUND); | |
} | |
return $this->result['results'][0]['geometry']['location']['lng']; | |
} | |
/** | |
* Converts a String to lowercase chars in the ASCII range | |
* | |
* It also replaces whitespace by a delimiter | |
* | |
* @param String $string The String to convert | |
* @param array $replace An array of Strings to replace in the String | |
* @param String $delimiter The delimiter used instead of whitespace | |
* | |
* @return String A lowercase, ASCII range String without whitespace | |
*/ | |
private function toLowerCaseAsciiDelimitedString( $string, $replace = array(), $delimiter = "-" ) | |
{ | |
if( !empty($replace) ) { | |
$string = str_replace((array)$replace, ' ', $string); | |
} | |
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $string); | |
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean); | |
$clean = strtolower(trim($clean, '-')); | |
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean); | |
return $clean; | |
} | |
} | |
/** | |
* AddressNotFoundException | |
*/ | |
class AddressNotFoundException extends Exception { public function __construct($message) {parent::__construct($message);} } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment