-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleGeoLocator.class.php
115 lines (103 loc) · 3.31 KB
/
GoogleGeoLocator.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/*
* Parsing data from google geo service. Free google developer account
* supports up to 2500 requests per day.
* More info about google maps api: https://developers.google.com/maps/documentation/geocoding/?hl=en
*
* @Version: 2.1
* @Author: Ondrej Podolinsky
* @Contact: [email protected]
*/
class GoogleGeoLocator extends GeoLocator {
/**
* Address for calling service
*/
const serviceAddress = 'https://maps.googleapis.com/maps/api/geocode/json?%s=%s&sensor=false&language=%s';
/**
* Language for results from google
*/
protected $language;
/**
* Initial set of base variables in class
*
* @param string $apiKey - api key for Google API
* @param string $lang - langugage for results from google
*/
public function __construct($apiKey = null,$lang = 'en') {
parent::__construct($apiKey);
$this->language = $lang;
}
/**
* Search by address/coords in concrete service
*
* @param string $paramVal
* @param string $paramName
* @return array - array with results. $results['error'] if any error.
*/
protected function searchByParam($paramVal, $paramName) {
$url = sprintf(self::serviceAddress, $paramName, $paramVal, $this->language);
if(!is_null($this->apiKey))
$url .= '&key=' . $this->apiKey;
$file = $this->getUrl($url);
$doc = json_decode($file);
$status = (string)$doc->status;
if($status == 'OK') {
$i = 0;
$street = $route = $locality = false;
foreach($doc->results as $res) {
if($res->types[0] == 'street_address')
$street = true;
elseif($res->types[0] == 'route')
$route = true;
elseif($res->types[0] == 'locality')
$locality = true;
}
if($street)
$route = $locality = false;
elseif($route)
$street = $locality = false;
elseif($locality)
$street = $route = false;
foreach($doc->results AS $res) {
if($res->types[0] == 'street_address' && $street || $res->types[0] == 'route' && $route || $res->types[0] == 'locality' && $locality) {
$results['results'][] = $this->parseResults($res);
$i++;
}
}
$results['count'] = $i;
} else {
$results['error'] = $status;
if(isset($doc->error_message))
$results['error'] .= ': '.$doc->error_message;
}
return $results;
}
/**
* Extract address + gps from document
*
* @param array $arr - one address element from service
* @return array - array with result
*/
protected function parseResults($arr) {
foreach($arr->address_components AS $c) {
if(count($c->types) > 0 && (string)$c->types[0] == 'street_number')
$house = $c->long_name;
elseif(count($c->types) == 0)
$tmpHouse = $c->long_name;
elseif((string)$c->types[0] == 'route')
$street = $c->long_name;
elseif((string)$c->types[0] == 'locality')
$result['city'] = $c->long_name;
elseif((string)$c->types[0] == 'country')
$result['country'] = $c->long_name;
elseif((string)$c->types[0] == 'postal_code')
$result['zip'] = $c->long_name;
}
$house = (!isset($house) && isset($tmpHouse)) ? $tmpHouse : '';
$result['lat'] = (float)$arr->geometry->location->lat;
$result['long'] = (float)$arr->geometry->location->lng;
$result['street'] = $street. ' '.$house;
$result['street'] = trim($result['street']);
return $result;
}
}