/
index.js
374 lines (325 loc) · 11 KB
/
index.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import { h, Component } from "preact";
import { route } from "preact-router";
import style from "./style";
import MapPane from './MapPane';
import Search from '../../components/Search';
import SearchResults from '../../components/SearchResults';
import { makeRequest, BASE_ENDPOINTS } from '../../js/server-requests-utils';
import { fetchAndDropUserPins, makePinMarkers, dropPin } from '../../js/saved-places';
import {createButton, createInput} from "../../js/utilities";
/**
* Leaflet related imports: leaflet, pouchdb module, and routing machine module
*/
import '../../../node_modules/leaflet/dist/leaflet.css';
import '../../../node_modules/leaflet-routing-machine/dist/leaflet-routing-machine.css';
import L from '../../js/leaflet-tileLayer-pouchdb-cached';
import Routing from '../../../node_modules/leaflet-routing-machine/src/index.js';
/**
* TIle layer configuration and attribution constants
*/
const OSM_URL = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
const OSM_ATTRIB =
'© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors';
const OSM_TILE_LAYER = new L.TileLayer(OSM_URL, {
attribution: OSM_ATTRIB,
useCache: true,
crossOrigin: true
});
// redirect marker icon path to assets directory
L.Icon.Default.imagePath = '../../assets/icons/leaflet/';
export default class LeafletOSMMap extends Component {
constructor(props) {
super(props);
this.state = {
map: null,
mapCenter: null,
userMarker: null,
droppedMarker: null, //used to track unsaved markers on map
userPosControlOnScreen: false,
defaultZoom: 16,
};
this.getUserLocation = this.getUserLocation.bind(this);
this.onLocationFound = this.onLocationFound.bind(this);
this.onLocationError = this.onLocationError.bind(this);
this.createCenterControl = this.createCenterControl.bind(this);
this.onMapClick = this.onMapClick.bind(this);
this.updateUserMarker = this.updateUserMarker.bind(this);
this.updateDroppedMarker = this.updateDroppedMarker.bind(this);
this.addPlaceDetailMarker = this.addPlaceDetailMarker.bind(this);
this.saveMarkerToDB = this.saveMarkerToDB.bind(this);
}
componentDidMount() {
// initialize map container if null
if (!this.state.map) {
this.setState({
map: L.map('map', {
zoomControl: false,
zoom: this.state.defaultZoom,
})
});
}
// add zoom to bottom left
const zoomControl = L.control
.zoom()
.setPosition('bottomleft')
.addTo(this.state.map);
this.state.map.addLayer(OSM_TILE_LAYER);
this.getUserLocation();
// configure map events
this.state.map.on('locationfound', this.onLocationFound);
this.state.map.on('locationerror', this.onLocationError);
this.state.map.on('click', this.onMapClick);
// once map is ready, drop pins (user=undefined --> default)
fetchAndDropUserPins(undefined, this.state.map, L);
// set map center accordingly if routed from home screen search
if (this.props.placeDetail &&
(this.state.mapCenter !== this.props.placeDetail.geometry.location)
) {
this.addPlaceDetailMarker(this.props.placeDetail.geometry.location,
this.props.placeDetail.place_id);
}
}
/**
* Handles updating marker identifying user's current location.
*
* @param {object} position geocode object with lat, lng keys
*/
updateUserMarker(position) {
let latlng = position;
// if (position.latlng) {
// latlng = position.latlng;
// }
const userMarker = L.circleMarker(latlng, {
radius: 8,
weight: 3,
fillColor: 'red'
})
.addTo(this.state.map)
.bindPopup('You Are Here');
this.setState({
userMarker: userMarker
});
// set "Center Map" control if not currently present
if (!this.state.userPosControlOnScreen) {
this.createCenterControl();
this.setState({ userPosControlOnScreen: true });
}
}
/**
* Updates the state of a dropped pin on map.
*
* @param {object} position geocode object with lat, lng keys
*/
updateDroppedMarker(position, placeID = '') {
const saveToDB = this.saveMarkerToDB;
let latlng = position;
if (position.latlng) {
latlng = position.latlng;
}
const droppedPin = L.marker(latlng, {
draggable: true,
autoPan: true
}).addTo(this.state.map);
if (this.state.droppedMarker) {
this.state.droppedMarker.remove();
}
this.setState({ droppedMarker: droppedPin });
const container = L.DomUtil.create('div');
const saveMarkerTitle = createInput('Title', 'markerInput', container);
const saveBtn = createButton('Save', '', container);
const deleteBtn = createButton('Remove', '', container);
//TODO: build function to handle description input using function as state
L.DomEvent.on(saveBtn, 'click', function () {
// const position = event.latlng;
const desc = saveMarkerTitle.value;
saveToDB(position, desc, placeID);
});
L.DomEvent.on(deleteBtn, 'click', function () {
droppedPin.remove();
});
droppedPin.bindPopup(container);
}
/**
* Adds a marker to map per information given by search results place details.
*
* @param {object} postion geocode object with lat, lng keys
* @param {string} placeID google map's place_id identifier
*/
addPlaceDetailMarker(position, placeID = '') {
this.setState({
mapCenter: position,
});
this.updateDroppedMarker(this.state.mapCenter, placeID);
this.state.map.setView(this.state.mapCenter, this.state.defaultZoom);
}
/**
* Attempt to get user's current location via device
*/
getUserLocation() {
if (this.props.userPosition) {
console.log('MapContainer: already have user location: ',
this.props.userPosition);
this.setState({ mapCenter: this.props.userPosition });
this.updateUserMarker(this.props.userPosition);
this.state.map.setView(this.state.mapCenter, this.state.defaultZoom);
} else {
console.log('MapContainer: getting user location');
this.state.map.locate({
setView: true,
enableHighAccuracy: false,
timeout: 60000,
maximumAge: Infinity,
});
}
}
/**
* Create control button for returning user to their current location
* on the map
*/
createCenterControl() {
L.Control.Center = L.Control.extend({
onAdd: map => {
const center = this.state.userMarker.getLatLng();
const container = L.DomUtil.create(
'div',
'leaflet-bar leaflet-control leaflet-control-custom'
);
const controlText = L.DomUtil.create('div');
controlText.style.color = 'rgb(25,25,25)';
controlText.style.fontFamily = 'Roboto,Arial,sans-serif';
controlText.style.fontSize = '16px';
controlText.style.lineHeight = '38px';
controlText.style.paddingLeft = '5px';
controlText.style.paddingRight = '5px';
controlText.innerHTML = 'Center Map';
container.appendChild(controlText);
L.DomEvent.disableClickPropagation(container);
container.style.backgroundColor = '#fff';
container.style.border = '2px solid #e7e7e7';
container.style.borderRadius = '3px';
container.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
container.style.cursor = 'pointer';
container.style.marginBottom = '22px';
container.style.textAlign = 'center';
container.title = 'Click to recenter the map';
L.DomEvent.on(container, 'mouseenter', function (e) {
e.target.style.background = '#e7e7e7';
});
L.DomEvent.on(container, 'mouseleave', function (e) {
// e.target.style.color = '#ccc';
e.target.style.background = '#fff';
});
L.DomEvent.on(container, 'click', function () {
map.setView(center, 16);
});
return container;
}
});
L.control.center = function (opts) {
return new L.Control.Center(opts);
};
L.control
.center()
.setPosition('bottomright')
.addTo(this.state.map);
}
/**
* Handle leaflet map get device location event
* @param {*} event
*/
onLocationFound(event) {
console.log('MapContainer: user position found: ', event.latlng);
this.setState({ mapCenter: event.latlng });
this.updateUserMarker(event.latlng);
if (this.props.selectedPin) {
this.setState({
mapCenter: {
lat: this.props.selectedPin.lat,
lng: this.props.selectedPin.lng
}
});
}
this.state.map.setView(this.state.mapCenter, this.state.defaultZoom);
}
/**
* Handle leaflet map get device location failure
* @param {*} err
*/
onLocationError(err) {
// TODO - alert message for now if don't have permission to get user
// user location. Next step is to notify and request permission again.
alert(err.message);
}
/**
* Attempts to save the marker to database.
*
* @param {object} position geocode object with lat, lng key values
* @param {string} desc user provide description for dropped marker
* @param {string} placeID google map's place_id identifier
*/
saveMarkerToDB(position, desc = '', placeID = '') {
if (desc == '') {
alert('Enter a description for the pin.');
return;
}
makeRequest('POST', 'savedPins', '', {
lat: position.lat,
lng: position.lng,
desc,
place_id: placeID,
}).then((response) => {
//remove old icon
this.state.droppedMarker.remove();
//add a new one
const savedMarker = makePinMarkers([response.data.pin], L);
dropPin(savedMarker, this.state.map);
// alert(`Succes: Saved pin at ${event.latlng} to db`);
}).catch((err) => {
switch (err.response.status) {
case 400: //duplicate pin
const origPin = err.response.data;
alert('This pin is already on your map.');
/*TO DO:
Convert popup alert to toast message
*/
break;
case 403: //user not signed in
/*TO DO:
Convert to overlay/lightbox window to sign up form
*/
if (confirm("Would you like to sign in to save places?")) {
route('/signin', true);
}
break;
default:
console.log(err); //error saving pin
}
});
}
/**
* On map click event, add a marker to the map at the clicked location.
*
* @param {object} event
*/
onMapClick(event) {
this.updateDroppedMarker(event.latlng);
}
render() {
return (
<div class={style.fullscreen}>
<Search position={this.state.mapCenter} map={this.state.map}
routeUrl={this.props.routeUrl} addMarker={this.addPlaceDetailMarker}>
<SearchResults />
</Search>
<MapPane paneHeight={this.props.paneHeight} />
</div>
);
}
componentWillUnmount() {
// TODO - add feature for tracking user's moving position, probably
// only needed for directions mode.
// map.stopWatch();
this.state.map.remove();
this.setState({ map: null });
}
}