first commit
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
class Geocode
|
||||
{
|
||||
public static function geocodeAddress($address)
|
||||
{
|
||||
global $httpAuthToken, $oauth_api;
|
||||
if (!empty($address)) {
|
||||
$data = http_build_query(
|
||||
array(
|
||||
'address' => $address,
|
||||
)
|
||||
);
|
||||
$url = $oauth_api . "geocode?" . $data;
|
||||
$opts = array(
|
||||
'http' => array(
|
||||
'method' => "GET",
|
||||
'timeout' => 60, /* 1 minute */
|
||||
'header' =>
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n" .
|
||||
"Accept: application/json\r\n" .
|
||||
"Authorization: Server-Token ${httpAuthToken}\r\n",
|
||||
),
|
||||
);
|
||||
$context = stream_context_create($opts);
|
||||
$body = file_get_contents($url, false, $context);
|
||||
$geocoded = json_decode($body, true);
|
||||
if (is_array($geocoded) && isset($geocoded["data"]) && !isset($geocoded["error"])) {
|
||||
return $geocoded["data"];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function reverseGPS($latitude, $longitude)
|
||||
{
|
||||
global $httpAuthToken, $oauth_api;
|
||||
|
||||
$body = null;
|
||||
if (!empty($latitude && !empty($longitude))) {
|
||||
$data = http_build_query(
|
||||
array(
|
||||
'lat' => $latitude,
|
||||
'lng' => $longitude,
|
||||
)
|
||||
);
|
||||
|
||||
$url = $oauth_api . "reverse?" . $data;
|
||||
$opts = array(
|
||||
'http' => array(
|
||||
'method' => "GET",
|
||||
'timeout' => 60, /* 1 minute */
|
||||
'header' =>
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n" .
|
||||
"Accept: application/json\r\n" .
|
||||
"Authorization: Server-Token ${httpAuthToken}\r\n",
|
||||
),
|
||||
);
|
||||
$context = stream_context_create($opts);
|
||||
$body = file_get_contents($url, false, $context);
|
||||
$geocoded = json_decode($body, true);
|
||||
|
||||
if (is_array($geocoded) && isset($geocoded["data"]) && !isset($geocoded["error"])) {
|
||||
return $geocoded["data"];
|
||||
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getTimeZone($lat, $lng)
|
||||
{
|
||||
global $googleKey;
|
||||
$result = array("message" => "Unexpected error", "dstOffset" => 0, "rawOffset" => 0, "status" => "Error", "timeZoneId" => "", "timeZoneName" => "");
|
||||
$url = "https://maps.googleapis.com/maps/api/timezone/json?location=${lat},${lng}×tamp=" . time() . "&key=" . $googleKey;
|
||||
$resp_json = file_get_contents($url);
|
||||
$resp = json_decode($resp_json, true);
|
||||
if ($resp["status"] == 'OK') {
|
||||
$result = $resp;
|
||||
if ($resp["timeZoneId"] != "") {
|
||||
$result["message"] = "";
|
||||
} else {
|
||||
$result["message"] = "Failed to get timezone offset for: ${lat},${lng}\n";
|
||||
}
|
||||
} else {
|
||||
$result["message"] = "Invalid service response code\n";
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function getGeoInfoBigDataCloud($latitude, $longitude)
|
||||
{
|
||||
global $city_keywords;
|
||||
$url = "https://api.bigdatacloud.net/data/reverse-geocode-client?latitude=" . $latitude . "&longitude=" . $longitude . "&localityLanguage=en";
|
||||
$opts = array(
|
||||
'http' => array(
|
||||
'method' => "GET",
|
||||
),
|
||||
);
|
||||
|
||||
$context = stream_context_create($opts);
|
||||
$body = file_get_contents($url, false, $context);
|
||||
$geoinfo = json_decode($body, true);
|
||||
if (count($geoinfo) > 0 && isset($geoinfo['localityInfo'])) {
|
||||
$localities = $geoinfo['localityInfo']['administrative'];
|
||||
foreach ($localities as $k => $v) {
|
||||
$name = isset($v['name']) ? $v['name'] : "";
|
||||
$description = isset($v['description']) ? $v['description'] : "";
|
||||
$info = strtolower($name . $description);
|
||||
foreach ($city_keywords as $key) {
|
||||
if (strpos($info, $key) !== false) {
|
||||
return $v['name'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user