Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import React, { useEffect, useRef,useState } from 'react';
  2. import L from 'leaflet';
  3. import {Button} from "react-bootstrap";
  4.  
  5. function GIS() {
  6. // create map
  7. const mapRef = useRef(null);
  8. const [markerPosition, setmarkerPosition] = useState([0,0])
  9.  
  10. useEffect(() => {
  11. mapRef.current = L.map('map', {
  12. center: [49.8419, 24.0315],
  13. zoom: 16,
  14. layers: [
  15. L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
  16. attribution:
  17. '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
  18. }),
  19. ]
  20. });
  21. }, []);
  22.  
  23.  
  24. // add marker
  25. const markerRef = useRef(null);
  26. useEffect(
  27. () => {
  28. console.log("2")
  29.  
  30. if (markerRef.current) {
  31. markerRef.current.setLatLng(markerPosition);
  32. } else {
  33. markerRef.current = L.marker(markerPosition).addTo(mapRef.current);
  34. }
  35. },
  36. [markerPosition]
  37. );
  38.  
  39. const handleClick = () => {
  40. console.log("1")
  41. //setmarkerPosition([45,45])
  42. markerRef.current = L.marker([45,45]).addTo(mapRef.current);
  43.  
  44. }
  45.  
  46. return (
  47. <>
  48. <Button onClick={handleClick}>ffffffffffffff</Button>
  49. <div ref={mapRef} id="map"></div>
  50. </>
  51. )
  52. }
  53.  
  54. export default GIS;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement