map.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. mapboxgl.accessToken = 'pk.eyJ1IjoidGluYW1hIiwiYSI6ImNpbWV2dDJudjAxM2Z0c2traGthOTJyZjYifQ.s_lP9rp2Iro7Urb4IZAzTQ';
  3. const bounds = [
  4. [-2.460270, 53.370427],
  5. [-2.062598, 53.552900]
  6. ]
  7. const map = new mapboxgl.Map({
  8. container: 'map',
  9. style: 'mapbox://styles/tinama/ck7avipta06nj1jmomolvvrbr',
  10. center: [-2.884808, 53.495743],
  11. zoom: 9.5,
  12. });
  13. const Draw = new MapboxDraw({
  14. displayControlsDefault: false,
  15. controls: {
  16. polygon: true,
  17. trash: true
  18. }
  19. });
  20. console.log("turf", turf);
  21. const AddPopup = (map) => (area) => {
  22. let polygon = turf.polygon(area);
  23. let center = turf.centroid(polygon);
  24. console.log(center, polygon)
  25. return new mapboxgl.Popup({closeOnClick: false})
  26. .setLngLat(center.geometry.coordinates)
  27. .setHTML(`
  28. <h3>form</h3>
  29. <label for="data">some data</label>
  30. <input type="text" class="form-control" id="data" placeholder="" required>
  31. <button type="button">save</button>
  32. `)
  33. .addTo(map)
  34. }
  35. map.addControl(Draw, 'top-right');
  36. map.on('load', function() {
  37. let drawnData = {};
  38. map.on('draw.create', createArea);
  39. map.on('draw.delete', deleteArea);
  40. map.on('draw.update', updateArea);
  41. function createArea(polygon) {
  42. let area = polygon.features[0].geometry;
  43. drawnData = {
  44. ...drawnData,
  45. [polygon.features[0].id]: area
  46. };
  47. console.log("create", area);
  48. const popup = AddPopup(map)(area.coordinates);
  49. }
  50. function deleteArea(polygon) {
  51. console.log('delete', polygon);
  52. }
  53. function updateArea(polygon) {
  54. console.log('update', polygon)
  55. }
  56. });