Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add possiblity to define the api key on geocode class #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions Service/Geocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ class Geocoder {
*
* @link http://code.google.com/apis/maps/documentation/geocoding/
*
* @param string $location
* @param boolean $simple If true, only the lat/lng will be returned
* @param string $location Location to geocode
* @param string $key API key
*
* @return GeocodeResult|GeocodeError
*/
public static function geocode( $location ) {
$response = self::scrapeAPI( $location );
public static function geocode( $location, $key = null ) {
$response = self::scrapeAPI( $location, $key);
if ( $response->status != 'OK' ) {
$error = new GeocodeError( $response->status, $location );
return $error;
Expand All @@ -47,12 +48,17 @@ public static function geocode( $location ) {
* Scrape the API
*
* @param string $location Location to geocode
* @param string $key API key
*
* @return GeocodeError|LatLng Returns a GeocodeError on error, LatLng on success.
*/
private static function scrapeAPI( $location ) {
$url = sprintf( "http://maps.google.com/maps/api/geocode/json?address=%s&sensor=false", urlencode( $location ) );
private static function scrapeAPI( $location, $key ) {
$url = sprintf( "https://maps.google.com/maps/api/geocode/json?address=%s&sensor=false", urlencode( $location ) );
if (isset($url)) {
$url .= "&key=" . $key;
}
$response = json_decode( Scraper::scrape( $url ) );
return $response;
}

}
}