<?php
	//@author sun
	if ($city == '' || $address == '') {return false;}
	switch ($city)
	{
		case 'hf':
			$lng = 117.282699;
			$lat = 31.866942;
			break;
		case 'sz':
			$lng = 120.602085;
			$lat = 31.303672;
			break;
		case 'wx':
			$lng = 120.31293;
			$lat = 31.563145;
			break;
		case 'xa':
			$lng = 108.953098;
			$lat = 34.2778;
			break;
		case 'cz':
			$lng = 119.981861;
			$lat = 31.813628;
			break;
		case 'cq':
			$lng = 106.555931;
			$lat = 29.566222;
			break;
		case 'wh':
			$lng = 118.381233;
			$lat = 31.329506;
			break;
		case 'hz':
			$lng = 120.165333;
			$lat = 30.269226;
			break;
		case 'ks':
			$lng = 120.98593;
			$lat = 31.384196;
			break;
		case 'sy':
			$lng = 123.438975;
			$lat = 41.81134;
			break;
		//其它城市扩展
		default:
	}
	
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body, html,#allmap {width: 80%;height: 80%;overflow: hidden;margin:0;}
</style>
<script src="http://www.house365.com/public/js/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>
<title>计算驾车时间与距离</title>
</head>
<body>
<br/><br/>
&nbsp;起点：<input type="text" id="startname" value="<?=$address?>">
终点：<input type="text" id="endname">
<input type="button" id="calculate" value="计算"><br/>
&nbsp;结果：<input type="text" id="result">公里
<br/><br/>
<div id="allmap"></div>
<script type="text/javascript">
	$(function() {
		var map = new BMap.Map("allmap");
		map.centerAndZoom(new BMap.Point(<?php echo $lng; ?>, <?php echo $lat; ?>), 12);
		//根据产品要求单位统一为公里
		$('#calculate').bind('click', function() {
			var startname = $('#startname').val();
			var endname = $('#endname').val();
			var strPattern = /^\s$/;
			if (startname == '' || strPattern.test(startname)) {
				alert('起点不能为空，请重新输入');
				$('#startname').focus();
				return false;
			}
			if (endname == '' || strPattern.test(endname)) {
				alert('终点不能为空，请重新输入');
				$('#endname').focus();
				return false;
			}
			transit.search($('#startname').val(), $('#endname').val());
		});

		var output = '';
		var searchComplete = function (results){
			if (transit.getStatus() != BMAP_STATUS_SUCCESS) {
				alert('无法计算出距离');
				return ;
			} else {
				var plan = results.getPlan(0);
				output = plan.getDistance(true);
				var distance;
				
				if (output.indexOf("公里") > 0) {
					 distance = output.replace('公里', '');
				} else {
					 distance = (Number)(output.replace('米', '')) / 1000;
				}
				$('#result').val(distance);
			}
		}
		var transit = new BMap.DrivingRoute(map, {renderOptions: {map: map},
			onSearchComplete: searchComplete
		});
	});
</script>
</body>
</html>
