-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwhat3words-map-marker.js
128 lines (102 loc) · 4.26 KB
/
what3words-map-marker.js
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
var map;
function MyLocationControl(controlDiv, map) {
// Set CSS for the control border.
var controlUI = document.createElement('div');
controlUI.style.cursor = 'pointer';
controlUI.style.backgroundColor = '#fff';
controlUI.style.margin = '0 10px 0 0';
controlUI.style.borderRadius = '3px';
controlUI.style.width = '30px';
controlUI.style.height = '30px';
controlUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
// controlUI.innerHTML =
controlUI.title = 'My location';
controlDiv.appendChild(controlUI);
// Setup the click event listeners: simply set the map to Chicago.
controlUI.addEventListener('click', function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(initialLocation);
});
}
});
}
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(35.137879, -82.836914),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoomControl: true,
mapTypeControl: true,
scaleControl: true,
streetViewControl: true
});
var myLocationControlDiv = document.createElement('div');
var myLocationControl = new MyLocationControl(myLocationControlDiv, map);
myLocationControlDiv.index = 1;
map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(myLocationControlDiv);
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
// map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
var myMarker = new google.maps.Marker({
position: new google.maps.LatLng(35.137879, -82.836914),
draggable: true
});
var markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
var bounds = new google.maps.LatLngBounds();
place = places[0];
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
map.fitBounds(bounds);
myMarker.setPosition(place.geometry.location);
what3WordsConvert(place.geometry.location.lat().toFixed(3), place.geometry.location.lng().toFixed(3));
});
setCurrentPosition(map, myMarker);
google.maps.event.addListener(myMarker, 'dragend', function (evt) {
what3WordsConvert(evt.latLng.lat().toFixed(3), evt.latLng.lng().toFixed(3));
});
google.maps.event.addListener(myMarker, 'dragstart', function (evt) {
document.getElementById('current').innerHTML = '<p>Currently dragging marker...</p>';
});
map.setCenter(myMarker.position);
myMarker.setMap(map);
}
var what3WordsConvert = function(lat, lng) {
what3words.positionToWords([lat, lng], function (ret) {
document.getElementById('current').innerHTML = '<p>'+ ret.join('.') +'</p>';
document.getElementById('three-word').value = ret.join('.');
});
}
var setCurrentPosition = function(map, marker, set3word) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(initialLocation);
marker.setPosition(initialLocation);
if (set3word) {
what3WordsConvert(position.coords.latitude.toFixed(3), position.coords.longitude.toFixed(3));
}
});
}
}