-
Notifications
You must be signed in to change notification settings - Fork 0
/
marker_drag.html
68 lines (60 loc) · 1.99 KB
/
marker_drag.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
#map {
position: absolute;
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<a href="https://onethinglab.wordpress.com/the-definitive-guide-to-google-maps-markers#marker-coordinates-moving">How to get coordinates when moving marker</a>
<div id="map"></div>
<script>
function add_marker(gmap, coordinates, text) {
var marker = new google.maps.Marker({
position: coordinates,
map: gmap,
draggable: true,
label: { text: text }
});
return marker;
}
function coords2text(coordinates) {
return 'latitude: ' + coordinates.lat() +
' longitude: ' + coordinates.lng();
}
function init_map() {
var places = {
'UA': { lat: 50.43333333, lng: 30.516667 },
'DE': { lat: 52.51666667, lng: 13.4 },
'GL': { lat: 64.18333333, lng: -51.75 },
'JP': { lat: 35.68333333, lng: 139.75 },
'MG': { lat: -18.91666667, lng: 47.516667 }
};
var gmap = new google.maps.Map(document.getElementById('map'),
{
zoom: 2,
center: { lat: -25.363, lng: 131.044 }
});
for (var country in places) {
var marker = add_marker(gmap, places[country], country);
marker.countryInfo = country;
google.maps.event.addListener(marker, 'dragstart', function (event) {
console.log(this.countryInfo + ': Start position: ', coords2text(this.getPosition()));
});
google.maps.event.addListener(marker, 'drag', function (event) {
console.log(this.countryInfo + ': Current position: ', coords2text(this.getPosition()));
});
google.maps.event.addListener(marker, 'dragend', function (event) {
console.log(this.countryInfo + ': Finished at: ', coords2text(this.getPosition()));
});
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?callback=init_map"></script>
</body>
</html>