Guest User

Untitled

a guest
Jun 24th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. <Map
  2. style={{
  3. width: '100%',
  4. backgroundColor: 'lightblue'
  5. }}
  6. ...
  7. // handling a react-leaflet map click event
  8. onClick={(event) => {
  9. this.onMapEvent('onMapClicked', {
  10. coords: [event.latlng.lat, event.latlng.lng]
  11. });
  12. }}
  13. ...
  14. />
  15.  
  16. // An event has occured in the react-leaflet map rendered by the webpage
  17. onMapEvent = (event, payload) => {
  18. // build a default payload consisting of map information if one is not provided
  19. if (!payload) {
  20. payload = {
  21. center: this.mapRef.current.leafletElement.getCenter(),
  22. bounds: this.mapRef.current.leafletElement.getBounds(),
  23. zoom: this.mapRef.current.leafletElement.getZoom()
  24. };
  25. }
  26.  
  27. this.sendMessage({
  28. event,
  29. payload
  30. });
  31. };
  32.  
  33. // data to send is an object containing key value pairs that will be
  34. // spread into the destination's state or seen as an event
  35. sendMessage = (payload) => {
  36. const message = JSON.stringify({
  37. prefix: MESSAGE_PREFIX,
  38. payload: payload
  39. });
  40.  
  41. if (document.hasOwnProperty('postMessage')) {
  42. document.postMessage(message, '*');
  43. } else if (window.hasOwnProperty('postMessage')) {
  44. window.postMessage(message, '*');
  45. } else {
  46. console.log('unable to find postMessage');
  47. }
  48. };
Add Comment
Please, Sign In to add comment