Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 3380c12b

Přidáno uživatelem horkym před téměř 7 roky(ů)

refs #6847: Cron - ziskani geolokace z google locations a ulozeni do databaze

Zobrazit rozdíly:

cron/location.php
11 11
    // Nepouzivane.
12 12
    // public $area;
13 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
    
14 22
    public function __construct($data) {
15 23
        $this->name = $data[0];
16 24
        $this->town = $data[1];
17 25
        $this->street = $data[2];
18 26
        $this->device = substr($data[3], 2);
19 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
        }
20 52
    }
21 53
    
22 54
}

Také k dispozici: Unified diff