1 |
a257bb07
|
horkym
|
<?php
|
2 |
|
|
|
3 |
|
|
class Location {
|
4 |
|
|
|
5 |
|
|
// Pouzivane atributy.
|
6 |
|
|
public $name;
|
7 |
|
|
public $town;
|
8 |
|
|
public $street;
|
9 |
|
|
public $device;
|
10 |
|
|
|
11 |
|
|
// Nepouzivane.
|
12 |
|
|
// public $area;
|
13 |
|
|
|
14 |
3380c12b
|
horkym
|
// --- ZEMEPISNA SIRKA A DELKA A UDAJE POTREBNE K JEJICH ZJISTENI. ---
|
15 |
|
|
|
16 |
|
|
private $key;
|
17 |
|
|
private $region;
|
18 |
|
|
|
19 |
|
|
public $lat;
|
20 |
|
|
public $lng;
|
21 |
|
|
|
22 |
a257bb07
|
horkym
|
public function __construct($data) {
|
23 |
|
|
$this->name = $data[0];
|
24 |
|
|
$this->town = $data[1];
|
25 |
|
|
$this->street = $data[2];
|
26 |
|
|
$this->device = substr($data[3], 2);
|
27 |
|
|
// $this->area = $data[4];
|
28 |
3380c12b
|
horkym
|
|
29 |
|
|
$this->key = "AIzaSyCSx7hyAzQiG5uocJTeZgf1Z3lpDy4kpEk";
|
30 |
|
|
$this->region = "cz";
|
31 |
|
|
$this->lat = -1;
|
32 |
|
|
$this->lng = -1;
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
// V pripade problemu, se ziskanim souboru protokolem HTTPS, v php.ini odkomentovat "extension=php_openssl.dll".
|
36 |
|
|
public function setGeolocation() {
|
37 |
|
|
$address = $this->town;
|
38 |
|
|
if ($this->town != $this->street) {
|
39 |
|
|
$address .= " ".$this->street;
|
40 |
|
|
}
|
41 |
|
|
$address = str_replace(" ", "+", $address); // Nemusi byt, jen pro jistotu.
|
42 |
|
|
|
43 |
|
|
$json = file_get_contents("https://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=".$this->region."&key=".$this->key);
|
44 |
|
|
$json = json_decode($json, TRUE);
|
45 |
|
|
if ($json["status"] == "OK") {
|
46 |
|
|
$this->lat = $json["results"]["0"]["geometry"]["location"]["lat"];
|
47 |
|
|
$this->lng = $json["results"]["0"]["geometry"]["location"]["lng"];
|
48 |
|
|
} else {
|
49 |
|
|
$this->lat = -1;
|
50 |
|
|
$this->lng = -1;
|
51 |
|
|
}
|
52 |
a257bb07
|
horkym
|
}
|
53 |
|
|
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
?>
|