Guest User

Untitled

a guest
Nov 13th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. /* global mapboxgl */
  2.  
  3. import React, { Component } from 'react';
  4. import './styles/reactmap.css';
  5.  
  6. export default class ReactMap extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = { loaded: false };
  10. this.projectImage = this.projectImage.bind(this);
  11. this.unprojectCoords = this.unprojectCoords.bind(this);
  12. }
  13.  
  14. unprojectCoords(top, bottom, left, right) {
  15. const upLeft = this.map.unproject([left, top]);
  16. const upRight = this.map.unproject([right, top]);
  17. const downRight = this.map.unproject([right, bottom]);
  18. const downLeft = this.map.unproject([left, bottom]);
  19. return { upLeft, upRight, downRight, downLeft };
  20. }
  21.  
  22. projectImage(imgSize) {
  23. const uL = this.map.project([-180, 85]);
  24. const uR = this.map.project([180, 85]);
  25. const lR = this.map.project([180, -85]);
  26. const lL = this.map.project([-180,-85]);
  27.  
  28. const mapSize = { x: lR.y - uR.y, y: uR.x - uL.x };
  29.  
  30. // fit image width to map width and scale height
  31. let scaledImgX = mapSize.x;
  32. let scaledImgY = (imgSize.y * mapSize.x) / imgSize.x;
  33. if(scaledImgY > mapSize.y) {
  34. // the scaled image is the wrong ratio
  35. // fit image height to map height and scale width;
  36. scaledImgY = mapSize.y;
  37. scaledImgX = (imgSize.x * mapSize.y) / imgSize.y;
  38. const xLeftOver = mapSize.x - scaledImgX;
  39. const xPosLeft = Math.floor(xLeftOver / 2);
  40. const xPosRight = Math.floor(xPosLeft + scaledImgX);
  41. const coords = this.unprojectCoords(uL.y, lR.y, xPosLeft, xPosRight);
  42. return coords;
  43. } else {
  44. // scaled image is the correct proportion
  45. const yLeftOver = mapSize.y - scaledImgY;
  46. const yPosTop = Math.floor(yLeftOver / 2);
  47. const yPosBtm = Math.floor(yPosTop + scaledImgY);
  48. const coords = this.unprojectCoords(yPosTop, yPosBtm, uL.x, uR.x);
  49. return coords;
  50. }
  51. }
  52.  
  53. componentDidMount() {
  54. console.log('component mounted!');
  55. const { container, img, imgHeight, imgWidth, returnFn } = this.props;
  56. const mapConfig = {
  57. container: container,
  58. zoom: 0,
  59. renderWorldCopies: false
  60. };
  61.  
  62. mapboxgl.accessToken = MAPBOX_TOKEN;
  63. this.map = new mapboxgl.Map(mapConfig);
  64.  
  65. const { upLeft, upRight, downRight, downLeft } = this.projectImage({ x:imgWidth, y:imgHeight });
  66.  
  67. const ddStyle = {
  68. "version": 8,
  69. "sources": {
  70. "overlay": {
  71. "type": "image",
  72. "url": img,
  73. "coordinates": [
  74. [upLeft.lng, upLeft.lat],
  75. [upRight.lng, upRight.lat],
  76. [downRight.lng, downRight.lat],
  77. [downLeft.lng, downLeft.lat]
  78. ]
  79. }
  80. },
  81. "layers": [
  82. {
  83. "id": "overlay",
  84. "source": "overlay",
  85. "type": "raster",
  86. "paint": {"raster-opacity": 1}
  87. }
  88. ]
  89. };
  90.  
  91. this.map.setStyle(ddStyle);
  92. if(returnFn) returnFn( this.map );
  93. this.setState({loaded: true});
  94. }
  95.  
  96. componentWillUnmount() {
  97. console.log('removing map');
  98. this.map.remove();
  99. }
  100.  
  101. render() {
  102. const { container, img, imgHeight, imgWidth, returnFn } = this.props;
  103. if(this.state.loaded){
  104. this.map.resize();
  105. }
  106.  
  107. return (
  108. <div id={ container } className='map'></div>
  109. );
  110. }
  111. }
Add Comment
Please, Sign In to add comment