Географические координаты с карты Гугла по адресу

В таблице stores:

  • is_loc_checked - проверена точка?
  • lat, lng - широта и долгота
google-geo-check.php
<?php
class Geoposition {
 
  private $db;
  public function __construct() {
    require_once(__DIR__ . '/lib/DataBaseMysql.php');
    require(__DIR__ . '/../etc/mysql.php');
    $this->db = new DataBaseMysql($config);
  }
 
  public function run() {
    $this->initCurl();
    $query = "SELECT * FROM `stores` WHERE `is_loc_checked` = 0";
    $points = $this->db->SelectSet($query);
    foreach($points as $point) {
      $this->point($point);
    }
    die('Done');
  }
 
  private function point($point) {
    $uri = $this->getUri($point['address']);
    $location = $this->geoLocation($uri);
    $this->saveLocation($point, $location);
  }
 
  private function getUri($address) {
    return 'http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false&language=ru';
  }
 
  private function geoLocation($uri) {
    curl_setopt($this->curl, CURLOPT_URL, $uri);
    $response = curl_exec($this->curl);
    if(empty($response)) return false;
    $response = json_decode($response);
    if(!is_object($response)) return false;
    if(!isset($response->status)) return false;
    if($response->status == 'OVER_QUERY_LIMIT') die('Daily Limit is over');
    if($response->status != 'OK') return false;
 
    $location =& $response->results[0]->geometry->location;
 
    if(!isset($location)) return false;
    return $location;
  }
 
  private function saveLocation($point, $location) {
    $lat = $location === false ? '0' : addslashes($location->lat);
    $lng = $location === false ? '0' : addslashes($location->lng);
    $query = "UPDATE `stores` SET `lat` = '$lat', `lng` = '$lng', `is_loc_checked` = 1 WHERE `id` = ".(int)$point['id'];
    $this->db->query($query);
    echo $lat.' '.$lng.PHP_EOL;
  }
 
  private $curl;
  private function initCurl() {
    $this->curl = curl_init();
    curl_setopt($this->curl, CURLOPT_HEADER,           false );
    curl_setopt($this->curl, CURLOPT_RETURNTRANSFER,   true );
    curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT,   30 );
  }
 
}
 
$geoposition = new Geoposition();
$geoposition->run();
Печать/экспорт