Projekt

Obecné

Profil

Stáhnout (1.67 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 $locality;
18
    private $region;
19
    
20
    public $lat;
21
    public $lng;
22
    
23
    public function __construct($data) {
24
        $this->name = $data[0];
25
        $this->town = $data[1];
26
        $this->street = $data[2];
27
        $this->device = substr($data[3], 2);
28
        // $this->area = $data[4];
29
        
30
        $this->key = "AIzaSyCSx7hyAzQiG5uocJTeZgf1Z3lpDy4kpEk";
31
        $this->locality = "Plzeňský kraj";
32
        $this->region = "cz";
33
        $this->lat = -1;
34
        $this->lng = -1;
35
    }
36
    
37
    // V pripade problemu, se ziskanim souboru protokolem HTTPS, v php.ini odkomentovat "extension=php_openssl.dll".
38
    public function setGeolocation() {
39
        $address = $this->town;
40
        if ($this->town != $this->street) {
41
            $address .= " ".$this->street;
42
        }
43
        $address .= " ".$this->locality;
44
        $address = str_replace(" ", "+", $address); // Nemusi byt, jen pro jistotu.
45
        
46
        $json = file_get_contents("https://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=".$this->region."&key=".$this->key);
47
        $json = json_decode($json, TRUE);
48
        if ($json["status"] == "OK") {
49
            $this->lat = $json["results"]["0"]["geometry"]["location"]["lat"];
50
            $this->lng = $json["results"]["0"]["geometry"]["location"]["lng"];
51
        } else {
52
            $this->lat = -1;
53
            $this->lng = -1;
54
        }
55
    }
56
    
57
}
58

    
59
?>
(5-5/10)