-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw-line-between-2-points.html
174 lines (148 loc) · 4.32 KB
/
draw-line-between-2-points.html
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html>
<head>
<title>Draw line between 2 points</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body {
height: 75%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="info" style="text-align: center">
<h1>Draw line between 2 points</h1>
<h3>Right Click to select the first point and Left click to select the second point</h3>
</div>
<div id="map"></div>
<div id="footer" style="text-align:center; margin-top:20px">
<button onclick="reset()">reset</button>
<button onclick="save()">save</button>
<br/>
<p id="starting-txt"></p>
<p id="end-txt"></p>
<p id="error-txt"></p>
</div>
<script>
var latLngEnd = null;
var latLngStart = null;
var map;
var markers = [];
var lines = [];
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: {lat: 46.2276, lng: 2.2137}
});
map.addListener('rightclick', function(e) {
placeMarkerAndPanTo(e.latLng, map, 'right');
});
map.addListener('click', function(e) {
placeMarkerAndPanTo(e.latLng, map, 'left');
});
}
function clearMarkers() {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
markers =[]
}
function placeMarkerAndPanTo(latLng, map, eventMouse) {
clearMarkers();
if(eventMouse == 'right'){
latLngEnd = latLng;
}
if(eventMouse == 'left'){
latLngStart = latLng;
}
if(latLngEnd!=null){
var marker = new google.maps.Marker({
position: latLngEnd,
map: map,
icon: 'http://maps.google.com/mapfiles/ms/icons/red-dot.png'
});
markers.push(marker);
}
if(latLngStart!=null){
var marker = new google.maps.Marker({
position: latLngStart,
map: map,
icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
});
markers.push(marker);
}
if(latLngStart != null && latLngEnd !=null){
drawLine(latLngStart,latLngEnd);
}
}
function clearLines(){
for (var i = 0; i < lines.length; i++) {
lines[i].setMap(null);
}
lines =[]
}
function drawLine(){
clearLines()
var flightPlanCoordinates = [
{lat: latLngStart.lat(), lng: latLngStart.lng()},
{lat: latLngEnd.lat(), lng: latLngEnd.lng()}
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
lines.push(flightPath)
flightPath.setMap(map);
}
function reset() {
clearMarkers();
clearLines();
latLngEnd = null;
latLngStart = null;
document.getElementById("error-txt").innerHTML = "";
document.getElementById("starting-txt").innerHTML = "";
document.getElementById("end-txt").innerHTML = "";
}
function save(){
if(latLngStart == null && latLngEnd == null){
document.getElementById("error-txt").innerHTML = "Select at least on point";
}else{
document.getElementById("error-txt").innerHTML = "";
document.getElementById("starting-txt").innerHTML = "";
document.getElementById("end-txt").innerHTML = "";
}
if(latLngStart!=null){
var starting_latitude = latLngStart.lat();
var starting_longitude = latLngStart.lng();
console.log('Starting Point ' )
console.log('------------------------------------');
console.log('Latitude : ' + starting_latitude);
console.log('Longitude : ' + starting_longitude);
console.log();
document.getElementById("starting-txt").innerHTML = "Starting Point {" + starting_latitude + " , " + starting_longitude + " }";
}
if(latLngEnd!=null){
var end_latitude = latLngEnd.lat();
var end_longitude = latLngEnd.lng();
console.log('End Point ' );
console.log('------------------------------------');
console.log('Latitude : ' + end_latitude);
console.log('Longitude : ' + end_longitude);
document.getElementById("end-txt").innerHTML = "End Point {" + end_latitude + " , " + end_longitude + " }";
}
}
</script>
<script src=https://maps.googleapis.com/maps/api/js?key=YOUR_OWN_KEY&signed_in=true&callback=initMap
async defer>
</script>
</body>
</html>