PHPでは、 timezone_location_get()は、指定されたタイムゾーンの地理的位置情報を取得するための非常に実用的な機能です。多くの場合、タイムゾーンデータまたはタイムゾーン関連情報の表示に基づいてジオロケーションが必要なシナリオで使用されます。この記事では、timezone_location_get()関数の戻りデータ形式を詳細に分析し、これらのデータを効率的に処理する方法を調べます。
timezone_location_get()関数は、 datetimezoneオブジェクトをパラメーターとして受け入れ、タイムゾーンに対応する地理的位置情報配列を返します。特定の使用法は次のとおりです。
$timezone = new DateTimeZone('Asia/Shanghai');
$location = timezone_location_get($timezone);
print_r($location);
この関数は、次のような構造で連想配列を返します。
Array
(
[country_code] => CN
[latitude] => 31.22222
[longitude] => 121.45806
[comments] => Shanghai
)
country_code :country ISO 3166-1 alpha-2 2文字コード。
緯度:緯度、浮動小数点数、このタイムゾーンの中心点の程度の単位。
経度:経度、浮動小数点数、このタイムゾーンの中心点の程度の単位。
コメント:タイムゾーン、通常は主要な都市名の簡単な説明。
実際のアプリケーションでは、これらのデータのさらなる処理がしばしば必要です。
座標形式を変換します(程度と秒と2番目の間に変換)
緯度と経度に基づいて距離を計算します
国のコードに基づいて国名またはロゴを検索する
function decimalToDMS($decimal) {
$degrees = floor($decimal);
$minutes = floor(($decimal - $degrees) * 60);
$seconds = round((($decimal - $degrees) * 60 - $minutes) * 60, 2);
return "{$degrees}°{$minutes}'{$seconds}\"";
}
$latDMS = decimalToDMS($location['latitude']);
$lngDMS = decimalToDMS($location['longitude']);
echo "Latitude: $latDMS, Longitude: $lngDMS";
function haversineDistance($lat1, $lng1, $lat2, $lng2) {
$earthRadius = 6371; // 地球の半径,ユニットはキロメートルです
$dLat = deg2rad($lat2 - $lat1);
$dLng = deg2rad($lng2 - $lng1);
$a = sin($dLat / 2) * sin($dLat / 2) +
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
sin($dLng / 2) * sin($dLng / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
return $earthRadius * $c;
}
// 例:上海と北京の間の距離を計算します
$beijing = ['latitude' => 39.9042, 'longitude' => 116.4074];
$distance = haversineDistance($location['latitude'], $location['longitude'], $beijing['latitude'], $beijing['longitude']);
echo "上海と北京の間の距離は約です {$distance} キロメートル";
Country_codeは、単純な配列またはサードパーティライブラリを介して、国名全体に変換できます。
$countryNames = [
'CN' => '中国',
'US' => 'アメリカ合衆国',
'JP' => '日本',
// 要件に応じて拡張できます
];
$country = $countryNames[$location['country_code']] ?? '未知の国';
echo "タイムゾーンの国:$country";
TimeZone_location_get()関数によって提供される地理的情報は、シンプルで直感的であり、タイムゾーンに対応する地理的座標と国コードを迅速に取得するのに適しています。単純な座標変換と地理的計算を組み合わせて、さまざまな地理的情報表示と処理のニーズを満たすことができます。
<?php
$timezone = new DateTimeZone('Asia/Shanghai');
$location = timezone_location_get($timezone);
function decimalToDMS($decimal) {
$degrees = floor($decimal);
$minutes = floor(($decimal - $degrees) * 60);
$seconds = round((($decimal - $degrees) * 60 - $minutes) * 60, 2);
return "{$degrees}°{$minutes}'{$seconds}\"";
}
$latDMS = decimalToDMS($location['latitude']);
$lngDMS = decimalToDMS($location['longitude']);
echo "タイムゾーンの地理情報:\n";
echo "国コード: " . $location['country_code'] . "\n";
echo "緯度: $latDMS\n";
echo "経度: $lngDMS\n";
echo "述べる: " . $location['comments'] . "\n";
// 例:別の場所からの距離を計算します
function haversineDistance($lat1, $lng1, $lat2, $lng2) {
$earthRadius = 6371;
$dLat = deg2rad($lat2 - $lat1);
$dLng = deg2rad($lng2 - $lng1);
$a = sin($dLat/2) * sin($dLat/2) +
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
sin($dLng/2) * sin($dLng/2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
return $earthRadius * $c;
}
$beijing = ['latitude' => 39.9042, 'longitude' => 116.4074];
$distance = haversineDistance($location['latitude'], $location['longitude'], $beijing['latitude'], $beijing['longitude']);
echo "上海と北京の間の距離は約です {$distance} キロメートル\n";