Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. import React, {useEffect, useRef, useState} from "react";
  2. import styled from "styled-components";
  3.  
  4. const Root = styled.div`
  5. width: 100vw;
  6. height: 100vh;
  7. `;
  8.  
  9. let isMapScriptAppended = false;
  10. export const Map = (props: { statsByLocation: any }) => {
  11.  
  12. const mapDiv = useRef((null as unknown) as any);
  13. const map = useRef(null as unknown as any);
  14. const [isMapReady, setMapReady] = useState(false);
  15.  
  16. useEffect(() => {
  17. const initializeMap
  18. const append = async () => {
  19. if(!isMapScriptAppended){
  20. const mapScript = document.createElement("script");
  21. mapScript.src =
  22. "https://maps.googleapis.com/maps/api/js?key=AIzaSyAM5AQOAn6Yt5PrbqvhNHURd-Uvd61edhw&libraries=visualization";
  23. document.head.appendChild(mapScript);
  24. isMapScriptAppended = true;
  25. mapScript.addEventListener("load",
  26. }
  27. };
  28. append();
  29.  
  30.  
  31. mapScript.addEventListener("load", async e => {
  32. const data = [
  33. {
  34. address: "China",
  35. weight: 30
  36. },
  37.  
  38. { address: "Thailand", weight: 14 },
  39. { address: "Hong Kong", weight: 8 },
  40. { address: "Taiwan", weight: 8 },
  41. { address: "Japan", weight: 7 },
  42. { address: "Macau", weight: 7 }
  43. ];
  44.  
  45. const heatMapData = data.map((v, i) => ({
  46. ...v,
  47. location: new google.maps.LatLng(
  48. jsons[i].results[0].geometry.location.lat,
  49. jsons[i].results[0].geometry.location.lng
  50. )
  51. }));
  52.  
  53. const asd = [
  54. {
  55. location: new google.maps.LatLng(37.782, -122.447),
  56. weight: 0.5
  57. }
  58. ];
  59.  
  60. const wuhan = new google.maps.LatLng(30.5928, 114.3055);
  61.  
  62. map.current = new google.maps.Map(mapDiv.current, {
  63. center: wuhan,
  64. zoom: 5,
  65. mapTypeId: "roadmap",
  66. disableDefaultUI: true,
  67. styles: [
  68. { elementType: "all", stylers: [{ color: "#330605" }] },
  69. {
  70. featureType: "water",
  71. elementType: "geometry",
  72. stylers: [{ color: "#0d0d0d" }]
  73. },
  74. {
  75. featureType: "administrative.country",
  76. elementType: "geometry.stroke",
  77. stylers: [{ color: "#ff9995" }]
  78. },
  79. {
  80. featureType: "administrative.country",
  81. elementType: "labels.text.fill",
  82. stylers: [{ color: "#910f0c" }]
  83. }
  84. ]
  85. });
  86.  
  87. map.current.data.add({
  88. geometry: {
  89. lat: 30.5928,
  90. lng: 114.3055
  91. },
  92. id: "sample",
  93. properties: {
  94. magnitude: 20
  95. }
  96. });
  97.  
  98. map.current.data.setStyle(feature => {
  99. console.log("a");
  100. return {
  101. icon: {
  102. path: google.maps.SymbolPath.CIRCLE,
  103. scale: 50,
  104. fillColor: "#f00",
  105. strokeWeight: 0
  106. }
  107. };
  108. });
  109.  
  110. gMap.data.setStyle({
  111. icon: {
  112. path: google.maps.SymbolPath.CIRCLE,
  113. scale: 50,
  114. fillColor: "#f00",
  115. strokeWeight: 0
  116. }
  117. });
  118.  
  119. const heatmap = new google.maps.visualization.HeatmapLayer({
  120. data: heatMapData,
  121. map: gMap,
  122.  
  123. radius: 60
  124. });
  125. });
  126.  
  127. }, []);
  128.  
  129. return <Root ref={mapDiv}></Root>;
  130. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement