免再比啦,免費專業諮詢,提供土地登記
銀行房貸信貸、不動產稅法等服務,歡迎諮詢!
全民比價網提供經驗豐富搬家,專業服務團隊
動作準時快速,服務貼心,線上免費估價再送紙箱

首頁  •  j2h 論壇 • 程式設計討論     • 

[AJAX] google map找出兩點的距離(公里)

房東:阿傀
發表時間:2011-11-28


今天無聊,就逛逛了GOOGLE CODE裡面所供應的一些服務,其中發現GOOGLE CODE的一個項目是可以抓出滑鼠在地圖上的經緯度,而後在網路上找到以經緯度的座標找出兩點之間的距離,以公里為單位。該兩點的距離,是以兩點之間形成的直線的距離,而不是道路與道路上的距離,請不要搞混。


 


請看以下為我寫的程式碼範例:












1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82


<!--
You are free to copy and use this sample in accordance with the terms of the
Apache license (http://www.apache.org/licenses/LICENSE-2.0.html)
-->
 
<!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=utf-8"/>
<title>Google Maps V3 API Sample</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
 
function initialize() {
var mapDiv = document.getElementById('map-canvas');
var map = new google.maps.Map(mapDiv, {
//center: new google.maps.LatLng(37.4419, -122.1419),
center: new google.maps.LatLng(25.125121506012825, 121.53352743682859),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
 
var infoWindow = new google.maps.InfoWindow();
google.maps.event.addListener(map, 'click', function(event) {
bubbleWindow(map, infoWindow, event);
});
bubbleWindow(event)
}
cordinatesArr=new Array();
function bubbleWindow(map,infoWindow,event){
latLngArray=event.latLng.toString().replace(/[\s\(\)]/gi,'').split(',');
cordinatesArr.push({
lat:parseFloat(latLngArray[0]),
lng:parseFloat(latLngArray[1])
})
 
var html = 'The LatLng value is: ' + event.latLng + ' at zoom level ' + map.getZoom();
if(cordinatesArr.length>1){
prevCord=cordinatesArr[cordinatesArr.length-2];
nowCord=cordinatesArr[cordinatesArr.length-1];
var distance=getDistance(prevCord.lat, prevCord.lng, nowCord.lat, nowCord.lng)
html+='<br>The distance is :'+distance+ ' KM';
}
infoWindow.setContent(html);
infoWindow.setPosition(event.latLng);
infoWindow.open(map);
 
/*
infoWindow.setPosition(event.latLng);
infoWindow.open(map);
if(confirm('The LatLng value is: ' + event.latLng + ' at zoom level ' + map.getZoom())){
alert('yes');
}else{
alert('no');
}*/
}
 
function getDistance(Lat1, Long1, Lat2, Long2){ //Converted from http://www.dotblogs.com.tw/jeff-yeh/archive/2009/02/04/7034.aspx
 
ConvertDegreeToRadians=function(degrees){
return (Math.PI/180)*degrees;
}
var Lat1r = ConvertDegreeToRadians(Lat1);
var Lat2r = ConvertDegreeToRadians(Lat2);
var Long1r = ConvertDegreeToRadians(Long1);
var Long2r = ConvertDegreeToRadians(Long2);
 
var R = 6371; // Earth's radius (km)
var d = Math.acos(Math.sin(Lat1r) *
Math.sin(Lat2r) + Math.cos(Lat1r) *
Math.cos(Lat2r) *
Math.cos(Long2r-Long1r)) * R;
return d; // returned distance is in KM
}
 
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body style="font-family: Arial; border: 0 none;">
<div id="map-canvas" style="width: 500px; height: 400px"></div>
</body>
</html>



 


http://blog.cfsoho.com/?p=149





  • 贊助網站       

    廣利不動產-新板特區指名度最高、值得您信賴的好房仲
    您的托付,廣利用心為您服務
    廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲
    完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心!



  •  共 0 人回應

    姓名:
    佈告內容:
    其他選項: