Advertisement
yudhaez0212

Untitled

Sep 23rd, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.24 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.lng; // longitude
  121. const y1 = min_coords.lat; // latitude
  122.  
  123. const x2 = max_coords.lng; // latitude
  124. const y2 = max_coords.lat; // longitude
  125.  
  126. centerX = x1 + ((x2 - x1) / 2);
  127. centerY = y1 + ((y2 - y1) / 2);
  128. const centerLatLng = centerY+','+centerX
  129.  
  130. document.getElementById("infoV2").value = centerLatLng;
  131. // #end update code by yudha
  132.  
  133. document.getElementById("info").value = selectedShape.getPath().getArray().map((item) => "new google.maps.LatLng" + item);
  134. shape.setEditable(true);
  135. selectColor(shape.get('fillColor') || shape.get('strokeColor'));
  136. }
  137.  
  138. function deleteSelectedShape() {
  139. if (selectedShape) {
  140. selectedShape.setMap(null);
  141. }
  142. }
  143.  
  144. function selectColor(color) {
  145. selectedColor = color;
  146. document.getElementById("color").value = selectedColor;
  147. for (var i = 0; i < colors.length; ++i) {
  148. var currColor = colors[i];
  149. colorButtons[currColor].style.border = currColor == color ? '2px solid #789' : '2px solid #fff';
  150. }
  151.  
  152. // Retrieves the current options from the drawing manager and replaces the
  153. // stroke or fill color as appropriate.
  154. var polylineOptions = drawingManager.get('polylineOptions');
  155. polylineOptions.strokeColor = color;
  156. drawingManager.set('polylineOptions', polylineOptions);
  157.  
  158. var rectangleOptions = drawingManager.get('rectangleOptions');
  159. rectangleOptions.fillColor = color;
  160. drawingManager.set('rectangleOptions', rectangleOptions);
  161.  
  162. var circleOptions = drawingManager.get('circleOptions');
  163. circleOptions.fillColor = color;
  164. drawingManager.set('circleOptions', circleOptions);
  165.  
  166. var polygonOptions = drawingManager.get('polygonOptions');
  167. polygonOptions.fillColor = color;
  168. drawingManager.set('polygonOptions', polygonOptions);
  169. }
  170.  
  171. function setSelectedShapeColor(color) {
  172. if (selectedShape) {
  173. if (selectedShape.type == google.maps.drawing.OverlayType.POLYLINE) {
  174. selectedShape.set('strokeColor', color);
  175. } else {
  176. selectedShape.set('fillColor', color);
  177. }
  178. }
  179. }
  180.  
  181. function makeColorButton(color) {
  182. var button = document.createElement('span');
  183. button.className = 'color-button';
  184. button.style.backgroundColor = color;
  185. google.maps.event.addDomListener(button, 'click', function () {
  186. selectColor(color);
  187. setSelectedShapeColor(color);
  188. });
  189.  
  190. return button;
  191. }
  192.  
  193. function buildColorPalette() {
  194. var colorPalette = document.getElementById('color-palette');
  195. for (var i = 0; i < colors.length; ++i) {
  196. var currColor = colors[i];
  197. var colorButton = makeColorButton(currColor);
  198. colorPalette.appendChild(colorButton);
  199. colorButtons[currColor] = colorButton;
  200. }
  201. selectColor(colors[0]);
  202. }
  203. var map;
  204.  
  205. var infoWindow;
  206.  
  207. function initialize2() {
  208. var myLatLng = new google.maps.LatLng(-5.494409, 104.623181);
  209. var mapOptions = {
  210. zoom: 17,
  211. center: myLatLng,
  212. mapTypeId: google.maps.MapTypeId.TERRAIN
  213.  
  214. };
  215. map = new google.maps.Map(document.getElementById('map'), mapOptions);
  216.  
  217. var bermudaTriangle;
  218. var triangleCoords = [
  219. ];
  220. bermudaTriangle = new google.maps.Polygon({
  221. paths: triangleCoords,
  222. strokeColor: '#FF0000',
  223. strokeOpacity: 0.8,
  224. strokeWeight: 3,
  225. fillColor: '#FF0000',
  226. fillOpacity: 0.35
  227. });
  228. bermudaTriangle.setMap(map);
  229.  
  230. var polyOptions = {
  231. strokeWeight: 0,
  232. fillOpacity: 0.45,
  233. editable: true
  234. };
  235. // Creates a drawing manager attached to the map that allows the user to draw
  236. // markers, lines, and shapes.
  237. drawingManager = new google.maps.drawing.DrawingManager({
  238. drawingMode: google.maps.drawing.OverlayType.POLYGON,
  239. markerOptions: {
  240. draggable: true
  241. },
  242. polylineOptions: {
  243. editable: true
  244. },
  245. rectangleOptions: polyOptions,
  246. circleOptions: polyOptions,
  247. polygonOptions: polyOptions,
  248. map: map
  249. });
  250.  
  251. google.maps.event.addListener(drawingManager, 'overlaycomplete', function (e) {
  252. if (e.type != google.maps.drawing.OverlayType.MARKER) {
  253. // Switch back to non-drawing mode after drawing a shape.
  254. drawingManager.setDrawingMode(null);
  255.  
  256. // Add an event listener that selects the newly-drawn shape when the user
  257. // mouses down on it.
  258.  
  259. var newShape = e.overlay;
  260. newShape.type = e.type;
  261. google.maps.event.addListener(newShape, 'click', function () {
  262. setSelection(newShape);
  263. });
  264. setSelection(newShape);
  265. }
  266. });
  267.  
  268. // Clear the current selection when the drawing mode is changed, or when the
  269. // map is clicked.
  270. google.maps.event.addListener(drawingManager, 'drawingmode_changed', clearSelection, getPolygonCoords);
  271. google.maps.event.addListener(map, 'click', clearSelection, getPolygonCoords);
  272. google.maps.event.addDomListener(document.getElementById('delete-button'), 'click', deleteSelectedShape);
  273.  
  274. buildColorPalette();
  275. }
  276. google.maps.event.addDomListener(window, 'load', initialize2);
  277.  
  278.  
  279.  
  280. //Display Coordinates below map
  281. function getPolygonCoords() {
  282. var len = myPolygon.getPath().getLength();
  283. var htmlStr = "";
  284. for (var i = 0; i < len; i++) {
  285. htmlStr += "new google.maps.LatLng(" + myPolygon.getPath().getAt(i).toUrlValue(5) + "), ";
  286. //Use this one instead if you want to get rid of the wrap > new google.maps.LatLng(),
  287. //htmlStr += "" + myPolygon.getPath().getAt(i).toUrlValue(5);
  288. }
  289. document.getElementById('info').innerHTML = htmlStr;
  290. }
  291.  
  292. function copyToClipboard(text) {
  293. window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
  294. }
  295. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement