Skip to content

Instantly share code, notes, and snippets.

@k-holy
Created November 11, 2011 04:40
Show Gist options
  • Save k-holy/1357212 to your computer and use it in GitHub Desktop.
Save k-holy/1357212 to your computer and use it in GitHub Desktop.
日出没時刻をGoogle Geocoding APIで検索
<?php
namespace Holy\Example;
const MAP_API_URL = 'http://maps.google.com/maps/api/geocode/json?sensor=false&region=jp';
use Holy\Example\Util as U;
class Util {
public static function H($data, $default=null)
{
$var = $default;
if (isset($data) && strcmp($data, '') != 0) {
$var = htmlspecialchars($data, \ENT_QUOTES, 'UTF-8');
}
return $var;
}
public static function make_time($yy, $mm, $dd, $hh=0, $mi=0, $ss=0)
{
if (checkdate($mm, $dd, $yy)) {
$time = mktime($hh, $mi, $ss, $mm, $dd, $yy);
if (is_int($time) && $time > 0) {
return $time;
}
}
throw new \RuntimeException('invalid date');
}
public static function build_options($start, $end, $step, $format, $selected=null)
{
return implode("\n", array_map(function($value) use ($format, $selected) {
return sprintf('<option value="%s"%s>%s</option>',
sprintf($format, $value),
((int)$selected === (int)$value) ? 'selected="selected"' : '',
$value);
}, range($start, $end, $step)));
}
}
$data = array();
$CURRENT_TIME = time();
$data['year'] = (isset($_GET['y']) && strlen($_GET['y']) >= 1)
? $_GET['y'] : date('Y', $CURRENT_TIME);
$data['month'] = (isset($_GET['m']) && strlen($_GET['m']) >= 1)
? $_GET['m'] : date('m', $CURRENT_TIME);
$data['day'] = (isset($_GET['d']) && strlen($_GET['d']) >= 1)
? $_GET['d'] : date('d', $CURRENT_TIME);
$data['latitude'] = (isset($_GET['lat']) && strlen($_GET['lat']) >= 1)
? $_GET['lat'] : ini_get('date.default_latitude');
$data['longitude'] = (isset($_GET['lng']) && strlen($_GET['lng']) >= 1)
? $_GET['lng'] : ini_get('date.default_longitude');
$data['address'] = (isset($_GET['addr']) && strlen($_GET['addr']) >= 1)
? $_GET['addr'] : null;
if (isset($_GET['addr_search']) && isset($data['address'])) {
$response = json_decode(file_get_contents(sprintf('%s&address=%s', MAP_API_URL,
rawurlencode($data['address']))));
if (isset($response->status) && $response->status === 'OK') {
if (isset($response->results[0]->geometry->location->lat)) {
$data['latitude'] = $response->results[0]->geometry->location->lat;
}
if (isset($response->results[0]->geometry->location->lng)) {
$data['longitude'] = $response->results[0]->geometry->location->lng;
}
}
} elseif (isset($_GET['latlng_search']) && isset($data['latitude']) && isset($data['longitude'])) {
$response = json_decode(file_get_contents(sprintf('%s&latlng=%s,%s', MAP_API_URL,
rawurlencode($data['latitude']), rawurlencode($data['longitude']))));
if (isset($response->status) && $response->status === 'OK') {
if (isset($response->results[0]->formatted_address)) {
$data['address'] = $response->results[0]->formatted_address;
}
}
}
$target_time = U::make_time((int)$data['year'], (int)$data['month'], (int)$data['day']);
$data['sunrize'] = date_sunrise($target_time, SUNFUNCS_RET_STRING,
$data['latitude'], $data['longitude']);
$data['sunset'] = date_sunset($target_time, SUNFUNCS_RET_STRING,
$data['latitude'], $data['longitude']);
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var create_map = function(){
var latlng = new google.maps.LatLng(
document.getElementById('latitude').value,
document.getElementById('longitude').value);
var map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
};
</script>
<title>日出没時刻</title>
</head>
<body onload="create_map();">
<h1>日出没時刻</h1>
<form method="get" action="<?=U::H($_SERVER['SCRIPT_NAME'])?>">
<input type="hidden" id="latitude" value="<?=U::H($data['latitude'])?>" />
<input type="hidden" id="longitude" value="<?=U::H($data['longitude'])?>" />
<p>
<select name="y">
<?=U::build_options(
intval(date('Y', $CURRENT_TIME) - 5),
intval(date('Y', $CURRENT_TIME) + 5), 1, '%d', $data['year'])?>
</select>年
<select name="m">
<?=U::build_options(1, 12, 1, '%02d', $data['month'])?>
</select>月
<select name="d">
<?=U::build_options(1, 31, 1, '%02d', $data['day'])?>
</select>日
</p>
<p>緯度<input type="text" name="lat" value="<?=U::H($data['latitude'])?>" /></p>
<p>経度<input type="text" name="lng" value="<?=U::H($data['longitude'])?>"/></p>
<p><input type="submit" name="latlng_search" value="緯度経度から調べる" /></p>
<p>住所<input type="text" name="addr" value="<?=U::H($data['address'])?>" /></p>
<p><input type="submit" name="addr_search" value="住所から調べる" /></p>
<p>日出<?=U::H($data['sunrize'])?></p>
<p>日没<?=U::H($data['sunset'])?></p>
</form>
<div id="map_canvas" style="width:200px; height:200px;"></div>
</body>
</html>
@k-holy
Copy link
Author

k-holy commented Nov 11, 2011

http://d.hatena.ne.jp/gallu/20111110/p1 からインスパイア
date_sunrise(), date_sunset()を使ってみたかっただけという…
なんかちっこい地図が出てるのは、Google Geocoding API単体で使うとNGだからで、特に意味はないよ^o^
ちなみに、php.iniのdate.default_latitudeとdate.default_longitudeのデフォルト値はエルサレムでした。PAAMAYIM_NEKUDOTAYIM!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment