Created
March 2, 2012 22:43
-
-
Save ganchiku/1961995 to your computer and use it in GitHub Desktop.
jalan-vc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$token = 'TOKEN'; | |
// 使用するデータ | |
$shops = array( | |
array('id' => 1, 'name' => 'ダイブサプライ', 'address' => '西牟婁郡白浜町2927-366-3', 'latitude' => '33.6781645', 'longitude' => '135.3481343'), | |
array('id' => 2, 'name' => 'バブルリングダイバーズ串本', 'address' => '東牟婁郡串本町串本617-36', 'latitude' => '33.4768918', 'longitude' => '135.7748795'), | |
array('id' => 3, 'name' => 'ミスオーシャン ダイビングサービス', 'address' => '西牟婁郡白浜町臨海410-1', 'latitude' => '33.6923658', 'longitude' => '135.3409542'), | |
array('id' => 4, 'name' => 'バディ・バディ', 'address' => '和歌山県和歌山市太田2-7-9', 'latitude' => '34.2303537', 'longitude' => '135.1945868'), | |
array('id' => 5, 'name' => 'スタードルフィンズ', 'address' => '和歌山市狐島19-1 ユニオンビル2F', 'latitude' => '34.2483118', 'longitude' => '135.151809'), | |
array('id' => 6, 'name' => 'サンマリン', 'address' => '日高郡みなべ町埴田1421-20', 'latitude' => '33.7493578', 'longitude' => '135.3273781'), | |
array('id' => 7, 'name' => 'インディゴブルー', 'address' => '田辺市芳養町288', 'latitude' => '33.7454587', 'longitude' => '135.3446626'), | |
array('id' => 8, 'name' => 'スクーバサポートサービス', 'address' => '日高郡由良町大引3-2', 'latitude' => '33.9666778', 'longitude' => '135.0861541'), | |
); | |
// パラメータ取得 | |
if (isset($_GET['id']) and is_numeric($_GET['id'])) { | |
$id = (int)$_GET['id']; | |
} else { | |
$id = 1; | |
} | |
// URL 構築 | |
$url = null; | |
$selected = null; | |
foreach ($shops as $shop) { | |
if ($shop['id'] == $id) { | |
$url = sprintf("http://ws.valuecommerce.ne.jp/travel/search?&serv_type=1&token=%s&format=%s&sort_by=%s&radius=%d&lat=%f&lng=%f", | |
$token, | |
'json', | |
'distance', | |
100000, | |
$shop['latitude'], | |
$shop['longitude'] | |
); | |
$selected = $shop; | |
break; | |
} | |
} | |
if (!isset($url)) { | |
throw Exception("IDが無効です。"); | |
} | |
// API 呼び出し。キャッシュがあり、更新時間が1日(86400秒)以内であれば、キャッシュを使用 | |
$cachepath = sprintf("%s/%s/shop_%d.json", dirname(__FILE__), "data", $selected['id']); | |
if (is_readable($cachepath) and filemtime($cachepath) + 86400 > time() ) { | |
$json = file_get_contents($cachepath); | |
} else { | |
$json = @file_get_contents($url); | |
if (!$json) { | |
throw Exception("APIの呼び出しに失敗しました。"); | |
} | |
file_put_contents($cachepath, $json); | |
} | |
$data = json_decode($json); | |
if (count($data->items) < 1) { | |
throw Exception('検索結果は0です。'); | |
} | |
// テンプレート | |
?> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>バリューコマースウェブサービステスト - じゃらん</title> | |
</head> | |
<body> | |
<?php foreach ($shops as $shop): ?> | |
<a href="jalan.php?id=<?php echo $shop['id'] ?>"><?php echo $shop['name'] ?></a> / | |
<?php endforeach; ?> | |
<hr /> | |
<h1>「<?php echo $selected['name'] ?>」に近いホテル</h1> | |
<div style="float:left; width:45%"> | |
<?php foreach ($data->items as $item): ?> | |
<div><img src="<?php echo $item->hotel->images[0]->imageSet[0]->free->url ?>" width="90px" /><br /> | |
<h4><a href="<?php echo $item->link ?>"><?php echo $item->title ?></a></h4> | |
<div><?php echo $item->hotel->add->JP ?></div> | |
</div> | |
<?php endforeach; ?> | |
</div> | |
<div id="map" style="float:left;width:45%;height:500px;border:1px #aaa solid"></div> | |
<hr style="clear:left"> | |
<div><a href="http://vcw.jp/">VCW ウェブサービス サンプル</a></div> | |
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
<script type="text/javascript"> | |
jQuery(document).ready(function() { | |
var map = new google.maps.Map($('#map').get(0), { | |
zoom: 14, | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
}); | |
var infowindow = new google.maps.InfoWindow; | |
var json = <?php echo $json ?>; | |
var latLng = null; | |
latLng = new google.maps.LatLng(<?php echo $selected['latitude'] . "," . $selected['longitude'] ?>); | |
var diveshopmarker = new google.maps.Marker({'position': latLng, 'icon': 'http://maps.google.co.jp/mapfiles/ms/icons/blue-dot.png'}); | |
diveshopmarker.setMap(map); | |
map.setCenter(latLng); | |
jQuery.each(json.items, function() { | |
latLng = new google.maps.LatLng(this.hotel.lat, this.hotel.lng); | |
var marker = new google.maps.Marker({'position': latLng }); | |
marker.content = { title: this.title, link: this.link, address: this.hotel.add.JP };; | |
(function (marker) { | |
new google.maps.event.addListener(marker, 'click', function() { | |
infowindow.setContent("<h3><a href='"+marker.content.link+"'>"+marker.content.title+"</a></h3><div>"+marker.content.address+"</div>"); | |
infowindow.open(map, marker); | |
}); | |
})(marker); | |
marker.setMap(map); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment