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>
|