<?php
	require("../__common__/__define_common.php");
	require("../abook/__define.php");
	require("../__common__/include-common-all.php");
	$category = $_GET['cat'];
	if (strstr($category, ':')) {
		$cate_ary = explode(':', $category);
		$category = $cate_ary[0];
		$sub_cate = $cate_ary[1];
	}
	$ne_lat = $_GET['ne_lat'];
	$sw_lat = $_GET['sw_lat'];
	$ne_lng = $_GET['ne_lng'];
	$sw_lng = $_GET['sw_lng'];
	$con = my_mysql_connect(_DB_SCHEMA_abook);
	$sql = 'select id_abook from m_abook';
	$sql .= ' where id_category = '.$category;
	if ($sub_cate.'' <> '') {
		if ($category == HYAKUSEN_CATEGORY_ID.'') {		/* 日本百選 */
			$sql .= " and c_memo = '".$sub_cate."'";
		} elseif ($category == HYAKUMEIZAN_CATEGORY_ID.'') {	/* 百名山 */
			$sql .= " and c_name2 = '".$sub_cate."'";
		}
	}
	if ($sw_lat < $ne_lat) {
		$sql .= ' and SUBSTRING(c_address1,1,LOCATE(",",c_address1)-1) BETWEEN '.$sw_lat.' and '.$ne_lat;
	} else {
		$sql .= ' and SUBSTRING(c_address1,1,LOCATE(",",c_address1)-1) BETWEEN '.$sw_lat.' and '.$ne_lat;
	}
	if (($ne_lng >= 0 and $sw_lng >= 0) or ($ne_lng < 0 and $sw_lng < 0)) {
		if ($ne_lng < $sw_lng) {
			$sql .= ' and SUBSTRING(c_address1,LOCATE(",",c_address1)+1) BETWEEN '.$ne_lng.' and '.$sw_lng;
		} else {
			$sql .= ' and SUBSTRING(c_address1,LOCATE(",",c_address1)+1) BETWEEN '.$sw_lng.' and '.$ne_lng;
		}
	} else {	/* 経度が0度、180度を跨る場合 */
		if ($ne_lng < 0) {
			$sql .= ' and ( SUBSTRING(c_address1,LOCATE(",",c_address1)+1) BETWEEN '.$sw_lng.' and 180.0';
			$sql .= ' or SUBSTRING(c_address1,LOCATE(",",c_address1)+1) BETWEEN -180.0 and '.$ne_lng. ' )';
		} else {
			$sql .= ' and ( SUBSTRING(c_address1,LOCATE(",",c_address1)+1) BETWEEN '.$sw_lng.' and 0.0';
			$sql .= ' or SUBSTRING(c_address1,LOCATE(",",c_address1)+1) BETWEEN 0.0 and '.$ne_lng. ' )';
		}
	}
	$rs = my_mysql_query($sql);
	$row = mysql_num_rows($rs);
	if ($row == 0) {
		echo '0';
		exit();
		error_exit('該当する場所がみつかりません。',False,False);
	}
	$id_list = '';
	while ($rec=mysql_fetch_array($rs)) {
		if ($id_list <> '') {
			$id_list .= ',';
		}
		$id_list .= $rec['id_abook'];
	}
	if ($category == HYAKUMEIZAN_CATEGORY_ID.'') {		/* 百名山 */
		$opt = 'hm';
		$so = 'c_yomi1';
	} elseif ($category == TETSUDO_EKI_CATEGORY_ID.'') {	/* 鉄道駅 */
		$opt = 'ts';
		$so = 'c_yomi1';
	} elseif ($category == SAKURA_MEISHO_CATEGORY_ID.'') {	/* 桜名所 */
		$opt = 'sk';
		$so = 'c_yomi1';
	} elseif ($category == HYAKUSEN_CATEGORY_ID.'') {	/* 日本百選 */
		$opt = 'hs';
		$so = 'c_yomi1';
	} elseif ($category == SEKAI_ISAN_CATEGORY_ID.'') {	/* 世界遺産 */
		$opt = 'wh';
		$so = 'c_name1';
	}
	$sql = "select * from v_abook where id_abook in (".$id_list.')';
	$sql .= ' order by '.$so;
	$rs = my_mysql_query($sql);
	$row = mysql_num_rows($rs);
	if ($row <> 0) {
		google_maps($rs, $row, $opt);
	} else {
		echo '0';
		exit();
	}
	$con = mysql_close($con);
	exit();
function google_maps($rs, $row, $opt) {
	while ($rec=mysql_fetch_array($rs)) {
		$popstr = '';
		$address = '';
		if ($opt == 'wh') {	// 世界遺産
			$name = quote_chg($rec['c_name1']);
			$name_left = explode('(', $rec['c_name1']);
			$popstr .= '≪<a href="javascript:window.open(`http://www.google.com/search?q='.urlencode($name_left[0]).'`);void(0);" style="color:#000080;font-weight:bold;">'.quote_chg($name).'</a>≫<br>';
			$popstr .= quote_chg($rec['c_name2']).'<br>';
			$popstr .= 'Ref.'.quote_chg($rec['c_phone1']).'&nbsp;&nbsp;';
			$popstr .= '<a href="javascript:window.open(`http://maps.google.com/maps?q=' . urlencode($rec['c_address1']);
			$popstr .= '`);void(0);">' . quote_chg($rec['c_address1']) . '</a><br>';
			$popstr .= '<p><a href="javascript:window.open(`http://whc.unesco.org/en/list/'. my_htmlspecialchars($rec['c_phone1']) . '`);void(0);">UNESCO</a>&nbsp;&nbsp;';
			$popstr .= '<a href="javascript:window.open(`http://translate.google.com/translate?prev=hp&hl=ja&js=y&u=http://whc.unesco.org/en/list/' . my_htmlspecialchars($rec['c_phone1']) . '`);void(0);">→日本語</a>';
			$popstr .= '</p>';
			$popstr .= quote_chg($rec['c_memo']).'<br>';
			$popstr .= quote_chg($rec['c_address2']).'<br>';
		} elseif ($opt == 'hm') {	// 百名山
			$name = quote_chg($rec['c_name1']);
			$name_left = explode('(', $rec['c_name1']);
			$popstr .= '≪<a href="javascript:window.open(`http://www.google.com/search?q='.urlencode($name_left[0]).'`);void(0);" style="color:#000080;font-weight:bold;">'.quote_chg($name).'</a>≫ <span style="color:#000080;">'.quote_chg($rec['c_name2']).'</span><br>';
			$popstr .= '<a href="javascript:window.open(`http://maps.google.com/maps?q=' . urlencode($rec['c_address1']);
			$popstr .= '`);void(0);">' . quote_chg($rec['c_address1']) . '</a><br>';
			$popstr .= quote_chg($rec['c_address2']).'<br>';
			$popstr .= '標高：'.quote_chg($rec['c_memo']).'m<br>';
		} elseif ($opt == 'sk') {	// 桜名木
			$name = quote_chg($rec['c_name1']);
			$name_left = explode('(', $rec['c_name1']);
			$popstr .= '≪<a href="javascript:window.open(`http://www.google.com/search?q='.urlencode($name_left[0]).'`);void(0);" style="color:#000080;font-weight:bold;">'.quote_chg($name).'</a>≫ <span style="color:#000080;">'.quote_chg($rec['c_name2']).'</span><br>';
			$popstr .= '<a href="javascript:window.open(`http://maps.google.com/maps?q=' . urlencode($rec['c_address1']);
			$popstr .= '`);void(0);">' . quote_chg($rec['c_address1']) . '</a><br>';
			$popstr .= quote_chg($rec['c_kenmei']).'<br>';
			$popstr .= quote_chg($rec['c_address2']).'<br>';
			if ($rec['c_phone1'] <> '') {
				$popstr .= '樹齢：'.quote_chg($rec['c_phone1']).'年<br>';
			}
			if ($rec['c_memo'] <> '') {
				$popstr .= '見頃：'.quote_chg($rec['c_memo']).'<br>';
			}
		} elseif ($opt == 'hs') {	// 日本百選
			$name = quote_chg($rec['c_name1']);
			$popstr .= '<a href="javascript:window.open(`http://www.google.com/search?q='.urlencode($rec['c_memo']).'`);void(0);" style="color:#0080E0;">'.quote_chg($rec['c_memo']).'</a><br>';
			$popstr .= '<a href="javascript:window.open(`http://www.google.com/search?q='.urlencode($rec['c_name1']).'`);void(0);" style="color:#000080;text-decoration:none;">≪<b>'.$name.'</b>≫</a><br>';
			$popstr .= '<a href="javascript:window.open(`http://maps.google.com/maps?q=' . urlencode($rec['c_address1']);
			$popstr .= '`);void(0);">' . quote_chg($rec['c_address1']) . '</a><br>';
			$popstr .= quote_chg($rec['c_address2']).'<br>';
		} elseif ($opt == 'ts') {	// 鉄道駅
			$rail = quote_chg($rec['c_memo']);
			$rail_left = explode('(', $rec['c_memo']);
			$popstr .= '<a href="javascript:window.open(`http://www.google.com/search?q='.urlencode($rail_left[0]).'`);void(0);" style="color:#000080;">'.$rail.'</a><br>';
			$name = quote_chg($rec['c_name1']);
			$name_left = explode('(', $rec['c_name1']);
			$popstr .= '≪<a href="javascript:window.open(`http://www.google.com/search?q='.urlencode($name_left[0]).'`);void(0);" style="color:#000080;font-weight:bold;">'.quote_chg($name).'</a>≫';
			$popstr .= '&nbsp;<a href="javascript:window.open(`http://transit.loco.yahoo.co.jp/station/time/search?q='.urlencode($name_left[0]).'`);void(0);" style="color:#006400;">→時刻表</a><br>';
			$popstr .= quote_chg($rec['c_kenmei']).'<br>';
			$popstr .= '<a href="javascript:window.open(`http://maps.google.com/maps?q=' . urlencode($rec['c_address1']);
			$popstr .= '`);void(0);">' . quote_chg($rec['c_address1']) . '</a><br>';
			$popstr .= quote_chg($rec['c_address2']).'<br>';
		} else {
			$name = quote_chg($rec['c_name1']).' '.quote_chg($rec['c_name2']);
			$popstr .= '<font color=#000080>≪<b>'.$name.'</b>≫</font><br>';
			$popstr .= quote_chg($rec['c_kenmei']).'<br>';
			$popstr .= '<a href="javascript:window.open(`http://maps.google.com/maps?q=' . urlencode($rec['c_address1']);
			$popstr .= '`);void(0);">' . quote_chg($rec['c_address1']) . '</a><br>';
			$popstr .= quote_chg($rec['c_memo']).'<br>';
			$popstr .= quote_chg($rec['c_address2']).'<br>';
		}
		$popstr .= '<a href="javascript:addMarker(\''.quote_chg($rec['c_name1']).'\','.$rec['c_address1'].')">マーカー作成</a><br><br>';
		if ($rec['c_markericon'] <> '') {
			$icon = $rec['c_markericon'];
		} else {
			$icon = $rec['c_categoryIcon'];
		}
		$address = $rec['c_address1'];
		echo trim($name).':::'.$address.':::'.$popstr."\n";
	}
}
?>
