<?php
	require("../__common__/__define_common.php");
	if (!defined("GOOGLE_API_KEY")) {
		define("GOOGLE_API_KEY", "ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q");
	}
	if (!defined("MAPS_FIX_HEIGHT")) {
		define("MAPS_FIX_HEIGHT", 38);
	}
	if ($_GET['addr'] <> '') {
		$addr = $_GET['addr'];
	} else {
		$addr = '';
	}
	$addr = str_replace(',', ' ', $addr);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Language" content="ja">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="author" content="ok.2nd">
<script language="javascript" src="../scripts/encodeURL.js" type="text/javascript"></script>
<title>Google Maps & Earth with Marker (不確実版)</title>
<script src="http://www.google.com/jsapi?key=<?= GOOGLE_API_KEY ?>"></script>
<script type="text/javascript">
var map;
var ge;
var geocoder = null;
var maxLat=-999, minLat=999, maxLng=-999, minLng=999;
google.load("maps", "2.x");
function setHeightPercent(elementID, fixHeight) {
	// マップの高さ設定
	if (document.all) {	// IE
		fixHeight += 40;
	}
	mapsWinHeight = 100 - Math.ceil( fixHeight * 100 / screen.availHeight );
	document.getElementById(elementID).style.height = mapsWinHeight + "%";
}
function init() {
	setHeightPercent('map3d', <?= MAPS_FIX_HEIGHT ?>);
	map = new GMap2(document.getElementById('map3d'));
	var mapui = map.getDefaultUI();
	mapui.maptypes.physical = true;
	map.setUI(mapui);
	map.addMapType(G_SATELLITE_3D_MAP);
	GEvent.addListener(map, 'maptypechanged', function() {
		if (ge) return;
		map.getEarthInstance(function(pluginInstance) {
			ge = pluginInstance;
			doStuffWithEarth();
		});
	});
	geocoder = new GClientGeocoder();
	addressSearch('<?= $addr ?>');
}
function addressSearch(addresses) {
	addresses = addresses.replace(/,/g, ' ');
	addresses = addresses.replace(/　/g, ' ');
	mark_addr = addresses.split(' ');
	maxLat = -999;
	minLat = 999;
	maxLng = -999;
	minLng = 999;
	map.clearOverlays();
	if (addresses != '' && mark_addr.length > 0) {
		var endp;
		for (i=0; i<mark_addr.length; i++) {
			if (i+1 == mark_addr.length) {endp = true};
			showMarker(mark_addr[i], endp);
		}
	} else {
		map.setCenter(new GLatLng(38.1000, 139.4385), 5);	// 日本地図
	}
}
function showMarker(address, endp) {
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (point) {
					var marker = new GMarker(point);
					var lat = point.lat();
					var lng = point.lng();
					if (maxLat < lat) maxLat = lat;
					if (minLat > lat) minLat = lat;
					if (maxLng < lng) maxLng = lng;
					if (minLng > lng) minLng = lng;
					GEvent.addListener(marker, "click", function() {
						html = address + '<br><br>';
						html += '緯度：' + lat + '<br>';
						html += '経度：' + lng + '<br><br>';
						html += '<a href="http://maps.google.com/maps?q=' + encodeURL(address);
						html += '" target="_blank">Googleマップ ≪' + address + '≫</a>';
						map.openInfoWindowHtml(point, html);
					});
					map.addOverlay(marker);
				//	if (endp) {
				//		setTimeout(function(){ 
		 					bounds = new GLatLngBounds(new GLatLng(minLat,minLng), new GLatLng(maxLat,maxLng));
							zoomlevel = map.getBoundsZoomLevel(bounds);
							if (zoomlevel > 15) zoomlevel = 15;
							map.setCenter(bounds.getCenter(), zoomlevel);
				//		},1000);
				//	}
				}
			}
		);
	}
}
function doStuffWithEarth() {
	document.getElementById('installed-plugin-version').innerHTML = ge.getPluginVersion().toString();
}
function markerClear() {
	map.clearOverlays();
}
function addressClear() {
	document.getElementById("address").value = '';
}
</script>
<style type="text/css">
/* * {margin: 0px; padding: 0px; } */
/*	↓ input 除く */
body, div, img, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, form, fieldset, textarea, p, blockquote, th, td, caption {
	margin: 0; 
	padding: 0;
}
input {
	margin: 0;
	padding: 0 3px;
	*padding: 0;
}
input[type="submit"] {
	background-color: #e0e0e0;
}
input[type="reset"] {
	background-color: #e0e0e0;
}
input[type="button"] {
	background-color: #e0e0e0;
}
button {
	background-color: #e0e0e0;
}
textarea,input,button {
	font-family: "ＭＳ Ｐゴシック",arial,helvetica,clean,sans-serif;
	line-height: 1.3;
	*line-height: 1.0;
	font-size: 100%;
}
select {
	font-family: "ＭＳ Ｐゴシック",arial,helvetica,clean,sans-serif;
	line-height: 1.3;
	*line-height: 1.0;
	font-size: 100%;
}
textarea {
	padding: 2px;
	_padding: 0px;
	/padding: 0px;
}
html {
	width: 100%;
	height: 100%;
	overflow: hidden;
}
body {
	margin: 0;
	width: 100%;
	height: 100%;
	font-size: 84%;
}
#form {
	margin: 5px 0 0 5px;
}
#map3d {
	width: 100%;
	height: 100%;
}
input.text {
	margin: 0px;
	padding: 1px 0 1px 0;
}
a { color: blue; }
</style>
</head>
<body onload="init()" onunload="GUnload()">
<form id="form" onsubmit="addressSearch(this.address.value); return false">
	<input id="address" class="text" type="text" size="80" name="address" value="<?= $addr ?>">
	<input type="submit" value="地図を検索">
	<button onclick="addressClear(); return false" style="margin-left:2px;">クリア</button>
	<button onclick="markerClear(); return false" style="margin-left:2px;">マーカー削除</button>
	<a href="http://maps.google.co.jp/" target="_blank" style="margin-left:16px;">Google マップ</a>
	<a href="http://code.google.com/intl/ja/apis/earth/" target="_blank" style="margin-left:8px;">Google Earth API</a>
</form>
<div id="map3d"></div>
<div>Installed Plugin Version: <span id="installed-plugin-version" style="font-weight: bold;">Loading...</span></div>
</body>
</html>
