Advertisement
yudhaez0212

Untitled

Sep 23rd, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. <style type="text/css">
  2. #map,
  3. html,
  4. body {
  5. padding: 0;
  6. margin: 0;
  7. height: 100%;
  8. }
  9.  
  10. #panel {
  11. width: 200px;
  12. font-family: Arial, sans-serif;
  13. font-size: 13px;
  14. float: right;
  15. margin: 10px;
  16. }
  17.  
  18. #color-palette {
  19. clear: both;
  20. }
  21.  
  22. .color-button {
  23. width: 14px;
  24. height: 14px;
  25. font-size: 0;
  26. margin: 2px;
  27. float: left;
  28. cursor: pointer;
  29. }
  30.  
  31. #delete-button {
  32. margin-top: 5px;
  33. }
  34. </style>
  35.  
  36. <script src="http://maps.google.com/maps/api/js?libraries=drawing"></script>
  37.  
  38. <script
  39. src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBbf5LFmJzolZcbdS5u3Mkut_y_GqjaVjA&callback=initMap&libraries=drawing&v=weekly"
  40. defer></script>
  41.  
  42. <div id="panel">
  43. <div id="color-palette"></div>
  44. <div>
  45. <button id="delete-button">Delete Selected Shape</button>
  46.  
  47. Isi Kode LatLng :
  48. <button id="clipboard-btn" onclick="copyToClipboard(document.getElementById('info').value)">Copy to
  49. Clipboard</button>
  50. <textarea id="info"></textarea>
  51.  
  52.  
  53. Isi Kode Warna :
  54. <button id="clipboard-btn" onclick="copyToClipboard(document.getElementById('color').value)">Copy to
  55. Clipboard</button>
  56. <textarea id="color"></textarea>
  57.  
  58.  
  59. Kode myLatLng = new google.maps.LatLng:
  60. <button id="clipboard-btn" onclick="copyToClipboard(document.getElementById('infoV2').value)">Copy to
  61. Clipboard</button>
  62. <textarea cols="25" id="infoV2"></textarea>
  63. </div>
  64. </div>
  65. <div id="map"></div>
  66. <script>
  67.  
  68. var drawingManager;
  69. var selectedShape;
  70. var colors = ['#1E90FF', '#FF1493', '#32CD32', '#FF8C00', '#4B0082'];
  71. var selectedColor;
  72. var colorButtons = {};
  73.  
  74. function clearSelection() {
  75. if (selectedShape) {
  76. selectedShape.setEditable(false);
  77. selectedShape = null;
  78. }
  79. }
  80.  
  81. function setSelection(shape) {
  82. clearSelection();
  83. selectedShape = shape;
  84.  
  85. // #start update code by yudha
  86. let tmpArrLatLong = [];
  87. let arrLatLong = [];
  88. selectedShape.getPath().getArray().map((item) => tmpArrLatLong.push("" + item));
  89. for (let i = 0; i < tmpArrLatLong.length; i++) {
  90. const item = tmpArrLatLong[i];
  91. const itemV2 = item.replace('(-', '-', item);
  92. const itemV3 = itemV2.replace(')', '', itemV2);
  93.  
  94. const array = itemV3.split(',');
  95. const finItem = {
  96. 'lat': parseFloat(array[0]),
  97. 'lng': parseFloat(array[1])
  98. };
  99.  
  100. arrLatLong.push(finItem);
  101. }
  102.  
  103. var lat = arrLatLong.map(function(p) {return p.lat});
  104. var lng = arrLatLong.map(function(p) {return p.lng});
  105.  
  106. var min_coords = {
  107. lat : Math.min.apply(null, lat),
  108. lng : Math.min.apply(null, lng)
  109. }
  110. var max_coords = {
  111. lat : Math.max.apply(null, lat),
  112. lng : Math.max.apply(null, lng)
  113. }
  114.  
  115. // x1, the lowest x coordinate
  116. // y1, the lowest y coordinate
  117. // x2, the highest x coordinate
  118. // y2, the highest y coordinate
  119.  
  120. const x1 = min_coords.lat;
  121. const y1 = min_coords.lng;
  122. const x2 = max_coords.lat;
  123. const y2 = max_coords.lng;
  124.  
  125. centerX = x1 + ((x2 - x1) / 2);
  126. centerY = y1 + ((y2 - y1) / 2);
  127. const centerLatLng = centerX+','+centerY
  128.  
  129. document.getElementById("infoV2").value = centerLatLng;
  130. // #end update code by yudha
  131.  
  132. document.getElementById("info").value = selectedShape.getPath().getArray().map((item) => "new google.maps.LatLng" + item);
  133. shape.setEditable(true);
  134. selectColor(shape.get('fillColor') || shape.get('strokeColor'));
  135. }
  136.  
  137. function deleteSelectedShape() {
  138. if (selectedShape) {
  139. selectedShape.setMap(null);
  140. }
  141. }
  142.  
  143. function selectColor(color) {
  144. selectedColor = color;
  145. document.getElementById("color").value = selectedColor;
  146. for (var i = 0; i < colors.length; ++i) {
  147. var currColor = colors[i];
  148. colorButtons[currColor].style.border = currColor == color ? '2px solid #789' : '2px solid #fff';
  149. }
  150.  
  151. // Retrieves the current options from the drawing manager and replaces the
  152. // stroke or fill color as appropriate.
  153. var polylineOptions = drawingManager.get('polylineOptions');
  154. polylineOptions.strokeColor = color;
  155. drawingManager.set('polylineOptions', polylineOptions);
  156.  
  157. var rectangleOptions = drawingManager.get('rectangleOptions');
  158. rectangleOptions.fillColor = color;
  159. drawingManager.set('rectangleOptions', rectangleOptions);
  160.  
  161. var circleOptions = drawingManager.get('circleOptions');
  162. circleOptions.fillColor = color;
  163. drawingManager.set('circleOptions', circleOptions);
  164.  
  165. var polygonOptions = drawingManager.get('polygonOptions');
  166. polygonOptions.fillColor = color;
  167. drawingManager.set('polygonOptions', polygonOptions);
  168. }
  169.  
  170. function setSelectedShapeColor(color) {
  171. if (selectedShape) {
  172. if (selectedShape.type == google.maps.drawing.OverlayType.POLYLINE) {
  173. selectedShape.set('strokeColor', color);
  174. } else {
  175. selectedShape.set('fillColor', color);
  176. }
  177. }
  178. }
  179.  
  180. function makeColorButton(color) {
  181. var button = document.createElement('span');
  182. button.className = 'color-button';
  183. button.style.backgroundColor = color;
  184. google.maps.event.addDomListener(button, 'click', function () {
  185. selectColor(color);
  186. setSelectedShapeColor(color);
  187. });
  188.  
  189. return button;
  190. }
  191.  
  192. function buildColorPalette() {
  193. var colorPalette = document.getElementById('color-palette');
  194. for (var i = 0; i < colors.length; ++i) {
  195. var currColor = colors[i];
  196. var colorButton = makeColorButton(currColor);
  197. colorPalette.appendChild(colorButton);
  198. colorButtons[currColor] = colorButton;
  199. }
  200. selectColor(colors[0]);
  201. }
  202. var map;
  203.  
  204. var infoWindow;
  205.  
  206. function initialize2() {
  207. var myLatLng = new google.maps.LatLng(-5.494409, 104.623181);
  208. var mapOptions = {
  209. zoom: 17,
  210. center: myLatLng,
  211. mapTypeId: google.maps.MapTypeId.TERRAIN
  212.  
  213. };
  214. map = new google.maps.Map(document.getElementById('map'), mapOptions);
  215.  
  216. var bermudaTriangle;
  217. var triangleCoords = [
  218. ];
  219. bermudaTriangle = new google.maps.Polygon({
  220. paths: triangleCoords,
  221. strokeColor: '#FF0000',
  222. strokeOpacity: 0.8,
  223. strokeWeight: 3,
  224. fillColor: '#FF0000',
  225. fillOpacity: 0.35
  226. });
  227. bermudaTriangle.setMap(map);
  228.  
  229. var polyOptions = {
  230. strokeWeight: 0,
  231. fillOpacity: 0.45,
  232. editable: true
  233. };
  234. // Creates a drawing manager attached to the map that allows the user to draw
  235. // markers, lines, and shapes.
  236. drawingManager = new google.maps.drawing.DrawingManager({
  237. drawingMode: google.maps.drawing.OverlayType.POLYGON,
  238. markerOptions: {
  239. draggable: true
  240. },
  241. polylineOptions: {
  242. editable: true
  243. },
  244. rectangleOptions: polyOptions,
  245. circleOptions: polyOptions,
  246. polygonOptions: polyOptions,
  247. map: map
  248. });
  249.  
  250. google.maps.event.addListener(drawingManager, 'overlaycomplete', function (e) {
  251. if (e.type != google.maps.drawing.OverlayType.MARKER) {
  252. // Switch back to non-drawing mode after drawing a shape.
  253. drawingManager.setDrawingMode(null);
  254.  
  255. // Add an event listener that selects the newly-drawn shape when the user
  256. // mouses down on it.
  257.  
  258. var newShape = e.overlay;
  259. newShape.type = e.type;
  260. google.maps.event.addListener(newShape, 'click', function () {
  261. setSelection(newShape);
  262. });
  263. setSelection(newShape);
  264. }
  265. });
  266.  
  267. // Clear the current selection when the drawing mode is changed, or when the
  268. // map is clicked.
  269. google.maps.event.addListener(drawingManager, 'drawingmode_changed', clearSelection, getPolygonCoords);
  270. google.maps.event.addListener(map, 'click', clearSelection, getPolygonCoords);
  271. google.maps.event.addDomListener(document.getElementById('delete-button'), 'click', deleteSelectedShape);
  272.  
  273. buildColorPalette();
  274. }
  275. google.maps.event.addDomListener(window, 'load', initialize2);
  276.  
  277.  
  278.  
  279. //Display Coordinates below map
  280. function getPolygonCoords() {
  281. var len = myPolygon.getPath().getLength();
  282. var htmlStr = "";
  283. for (var i = 0; i < len; i++) {
  284. htmlStr += "new google.maps.LatLng(" + myPolygon.getPath().getAt(i).toUrlValue(5) + "), ";
  285. //Use this one instead if you want to get rid of the wrap > new google.maps.LatLng(),
  286. //htmlStr += "" + myPolygon.getPath().getAt(i).toUrlValue(5);
  287. }
  288. document.getElementById('info').innerHTML = htmlStr;
  289. }
  290.  
  291. function copyToClipboard(text) {
  292. window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
  293. }
  294. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement