Advertisement
Guest User

Untitled

a guest
Apr 5th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. <html>
  2. <head>
  3. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  5. <title>Google Maps JavaScript API v3 Example: Polygon Arrays</title>
  6. <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
  7. <script type="text/javascript"
  8. src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=true">
  9. </script>
  10. <script type="text/javascript">
  11. // Permette di inserire i vertici di un poligono con dei click
  12. // Con un doppio click all'interno del poligono vengono visulizzate le coordinate
  13. // dei vertici e l'area della regione all'iterno del poligono.
  14. // Con un doppio click su i vertici questi vengono eliminati
  15. var map;
  16. var infoWindow;
  17. var poligono;
  18. var flag=0;
  19. function initialize() {
  20. var myLatLng = new google.maps.LatLng(45.77351, 8.828105);
  21. var myOptions = {
  22. zoom: 17,
  23. center: myLatLng,
  24. mapTypeId: google.maps.MapTypeId.SATELLITE
  25. };
  26.  
  27. var bermudaTriangle;
  28.  
  29. map = new google.maps.Map(document.getElementById("map_canvas"),
  30. myOptions);
  31.  
  32.  
  33. google.maps.event.addListener(map, 'click', aggiungiPunto);
  34.  
  35. infowindow = new google.maps.InfoWindow();
  36. }
  37.  
  38. function aggiungiPunto(event) {
  39. var nmark = 0;
  40. if (flag == 0) {
  41. var pathCoordinates = new google.maps.MVCArray();
  42. pathCoordinates.push(event.latLng);
  43. poligono = new google.maps.Polygon({
  44. paths: pathCoordinates,
  45. strokeColor: "#FF0000",
  46. strokeOpacity: 0.8,
  47. strokeWeight: 3,
  48. fillColor: "#FF0000",
  49. fillOpacity: 0.35
  50. });
  51. poligono.setMap(map);
  52. google.maps.event.addListener(poligono, 'dblclick', showArrays);
  53. flag = 1;
  54. }
  55. else {
  56. var path = poligono.getPath();
  57.  
  58. // Because path is an MVCArray, we can simply append a new coordinate
  59. // and it will automatically appear
  60. //path.push(event.latLng);
  61. nmark = maxArea(path, event.latLng);
  62. path.insertAt(nmark, event.latLng);
  63. }
  64. var marker = new google.maps.Marker({
  65. position: event.latLng,
  66. map: map
  67. });
  68. marker.setTitle(event.latLng.toString());
  69. //google.maps.event.addListener(marker, 'dblclick', showArrays);
  70. google.maps.event.addListener(marker, 'dblclick', function () {
  71. //alert(marker.getTitle());
  72. removeVertice(marker.getTitle());
  73. marker.setMap(null);
  74. //map.removeOverlay(marker);
  75. });
  76.  
  77. }
  78. function removeVertice(vertice) {
  79. var path = poligono.getPath();
  80. path.forEach(function (latLng, index) {
  81. if(latLng==vertice)
  82. {
  83. path.removeAt(index);
  84. return;
  85. }
  86. })
  87. }
  88. function showArrays(event) {
  89.  
  90. // Since this Polygon only has one path, we can call getPath()
  91. // to return the MVCArray of LatLngs
  92. var vertices = poligono.getPath();
  93.  
  94. var contentString = "<b>Vertici del poligono</b><br />";
  95. contentString += "Punto cliccato: <br />" + event.latLng.lat() + "," + event.latLng.lng() + "<br />";
  96.  
  97. vertices.forEach(function (latLng, index) {
  98. contentString += "<br />" + " [" + index + "] => " + latLng;
  99. });
  100. var area = google.maps.geometry.spherical.computeArea(vertices);
  101. contentString += "<br />" + " area==> " + area;
  102. // Replace our Info Window's content and position
  103. infowindow.setContent(contentString);
  104. infowindow.setPosition(event.latLng);
  105.  
  106. infowindow.open(map);
  107. }
  108.  
  109. function maxArea(vettore, coord) {
  110. var lung = vettore.length;
  111. var max = 0;
  112. var maxii=0;
  113. var area;
  114. for (var ii = 0; ii < lung; ii++) {
  115. area = getArea(vettore, coord, ii);
  116. if (area > max) {
  117. max = area;
  118. maxii = ii;
  119. }
  120. }
  121. return maxii;
  122. }
  123. function getArea(vettore, coord, i) {
  124. var appoggio = new google.maps.MVCArray();
  125. vettore.forEach(function (latLng, index) {
  126. if (index == i)
  127. appoggio.push(coord);
  128. appoggio.push(latLng);
  129. } )
  130. return google.maps.geometry.spherical.computeArea(appoggio);
  131. }
  132.  
  133. </script>
  134. </head>
  135. <body style="margin:0px; padding:0px;" onload="initialize()">
  136. <div id="map_canvas" style="width: 100%; height: 100%;"></div>
  137. </body>
  138. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement