Projekt

Obecné

Profil

Stáhnout (1.56 KB) Statistiky
| Větev: | Revize:
1
<?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
    // --- 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
    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
        
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&region=".$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
    }
53
    
54
}
55

    
56
?>
(3-3/7)