计算两个城市之间的距离


<!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" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312"/>
<title>Google 地图 计算两个城市之间的距离,非直线距离</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">

//计算两地之间的距离,非直线距离
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var response;
var directionsDisplay = new google.maps.DirectionsRenderer();
//不走高速
function noGaosu(){
var start = document.feng.sc.value;
var end = document.feng.ec.value;
var request = {
avoidHighways: true,
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(
request, function(response, status){
if (status == google.maps.DirectionsStatus.OK){
directionsDisplay.setDirections(response);
//m= response.routes[0].legs[0].distance.text;
//alert(m);
var distance = document.getElementById('nodistance');
distance.innerHTML = '不走高速的距离: '+response.routes[0].legs[0].distance.text;
}
}
);
}
//走高速
function gaosu(){
var start = document.feng.sc.value;
var end = document.feng.ec.value;
var request = {
avoidHighways: false,
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(
request, function(response, status){
if (status == google.maps.DirectionsStatus.OK){
directionsDisplay.setDirections(response);
//m= response.routes[0].legs[0].distance.text;
//alert(m);
var distance = document.getElementById('distance');
distance.innerHTML = '走高速的距离: '+response.routes[0].legs[0].distance.text;
}
}
);
}

</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<form name="feng">
<div id="map_canvas" style="width: 600px; height: 400px"></div>
<b>查询两个城市间的距离:</b><br />
出发城市: <input type="text" id="sc" /><br />
到达城市: <input type="text" id="ec" /><br />
<input type="button" value="高 速" id="bt" onClick="gaosu()"/>
<input type="button" value="不走高速" id="bt" onClick="noGaosu()"/>
<div id="distance"></div>
<div id="nodistance"></div>
</form>
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用经纬度坐标计算两个城市之间距离。 以下是使用Java计算两个城市之间距离的示例代码: ```java import java.lang.Math; public class DistanceCalculator { public static void main(String[] args) { // 定义两个城市的经纬度坐标 double lat1 = 31.2304; double lon1 = 121.4737; double lat2 = 39.9042; double lon2 = 116.4074; // 计算两个城市之间距离 double distance = calculateDistance(lat1, lon1, lat2, lon2); // 输出结果 System.out.println("两个城市之间距离为:" + distance + "公里"); } /** * 根据经纬度坐标计算两个之间距离 * @param lat1 点1的纬度 * @param lon1 点1的经度 * @param lat2 点2的纬度 * @param lon2 点2的经度 * @return 两个之间距离,单位:公里 */ public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) { final double EARTH_RADIUS = 6378.137; // 地球半径,单位:千米 double radLat1 = Math.toRadians(lat1); double radLat2 = Math.toRadians(lat2); double a = radLat1 - radLat2; double b = Math.toRadians(lon1) - Math.toRadians(lon2); double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))); s = s * EARTH_RADIUS; s = Math.round(s * 10000) / 10000; // 四舍五入保留4位小数 return s; } } ``` 该示例代码使用了Haversine公式来计算地球表面两个之间距离。需要注意的是,该算法的精度可能会受到地球形状的影响,在极端情况下可能会产生一些误差。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值