Skip to content

Commit

Permalink
initial class contains: (Geocode, Geolocation, searchPlace) with sett…
Browse files Browse the repository at this point in the history
…ers and abstract request class.
  • Loading branch information
travisfont committed Apr 21, 2016
1 parent af0e539 commit 979a1d2
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Google.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

require_once 'setters.php';

final class Google extends setters
{
public function Geocode()
{
$this->url = 'https://maps.googleapis.com/maps/api/geocode/json?';

switch (TRUE)
{
case (!empty($this->lat) && !empty($this->lng)):

$this->url .= 'latlng='.$this->lat.','.$this->lng;
break;

case (!empty($this->address)):

$this->url .= 'address='.$this->address;
break;
}

$this->url .= '&sensor='.$this->sensor
.'&key='.$this->apiKey;

return $this->getData();
}

public function Geolocation()
{
$this->url = 'https://www.googleapis.com/geolocation/v1/geolocate?'
.'query='.$this->query
.'&key='.$this->apiKey;

return $this->getData();
}

public function searchPlace()
{
$this->url = 'https://maps.googleapis.com/maps/api/place/textsearch/json?'
.'query='.$this->query
.'&sensor='.$this->sensor
.'&radius='.$this->radius
.'&key='.$this->apiKey;

return $this->getData();
}
}
22 changes: 22 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

require_once 'Google.php';

define('API_KEY', 'abcdefgh1230000000');

$google = new Google(API_KEY);
$google->setAddress('Bertrange, LU');
$results = $google->Geocode();

echo '<pre>';
print_r($results);

/* Searching places (Using location and store name)
$google = new Google(API_KEY);
$google->setQuery('Walmart, 33065, USA');
$results = $google->searchPlace();
echo '<pre>';
print_r($results);
#print_r($google->error);
*/
56 changes: 56 additions & 0 deletions request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

abstract class request
{
public $error = array();
protected $apiKey;

public function __construct($apiKey)
{
if (strlen($apiKey) > 38)
{
$this->apiKey = $apiKey;
}
else
{
$this->error[] = 'less than 38 character key length';
}
}

protected function getData()
{
if (!empty($this->url))
{
if ($string = file_get_contents($this->url))
{
$results = json_decode($string);

if (isset($results->results))
{
return $results->results;
}
elseif (isset($results))
{
return $results;
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}

public function showURL()
{
return $this->url;
}
}
60 changes: 60 additions & 0 deletions setters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

require_once 'request.php';

class setters extends request
{
protected $query;
protected $address;
protected $lat;
protected $lng;
protected $url;
protected $sensor = 'true';
protected $radius = 20;

public function setSensor($sensor)
{
if ($sensor === TRUE || $sensor == 'true')
{
$this->sensor = 'true';
}
if ($sensor === FALSE || $sensor == 'false')
{
$this->sensor = 'false';
}
}

public function setQuery($query)
{
$this->query = urlencode($query);
}

public function setRadius($radius)
{
if ((int) $radius > 0)
{
$this->radius = $radius;
}
}

public function setAddress($address)
{
$this->address = urlencode($address);
}

public function setLatLng($lat, $lng)
{
$this->lat = $lat;
$this->lng = $lng;
}

public function setLat($lat)
{
$this->lat = $lat;
}

public function setLng($lng)
{
$this->lng = $lng;
}
}

0 comments on commit 979a1d2

Please sign in to comment.