|
| 1 | +<?php |
| 2 | + |
| 3 | +// no direct access |
| 4 | +defined('_JEXEC') or die; |
| 5 | +//require_once dirname(__FILE__).'/helper.php'; |
| 6 | + |
| 7 | +$costokm = $params->get('eurokm'); |
| 8 | + |
| 9 | +$document = & JFactory::getDocument(); |
| 10 | +$document->addScript('http://maps.google.com/maps/api/js?sensor=true'); |
| 11 | + |
| 12 | +$document->addScriptDeclaration(" |
| 13 | + var location1; |
| 14 | + var location2; |
| 15 | +
|
| 16 | + var address1; |
| 17 | + var address2; |
| 18 | +
|
| 19 | + var latlng; |
| 20 | + var geocoder; |
| 21 | + var map; |
| 22 | +
|
| 23 | + var line; |
| 24 | +
|
| 25 | + var infowindow1; |
| 26 | + var infowindow2; |
| 27 | +
|
| 28 | + var distance; |
| 29 | +
|
| 30 | + function initialize(){ |
| 31 | + geocoder = new google.maps.Geocoder(); |
| 32 | +
|
| 33 | + address1 = document.getElementById(\"address1\").value; |
| 34 | + address2 = document.getElementById(\"address2\").value; |
| 35 | +
|
| 36 | + if (geocoder){ |
| 37 | + geocoder.geocode( { 'address': address1}, function(results, status){ |
| 38 | + if (status == google.maps.GeocoderStatus.OK){ |
| 39 | + location1 = results[0].geometry.location; |
| 40 | + } else { |
| 41 | + alert('Geocode was not successful for the following reason: ' + status); |
| 42 | + } |
| 43 | + }); |
| 44 | +
|
| 45 | + geocoder.geocode( { 'address': address2}, function(results, status){ |
| 46 | + if (status == google.maps.GeocoderStatus.OK){ |
| 47 | + location2 = results[0].geometry.location; |
| 48 | + showMap(); |
| 49 | + } else { |
| 50 | + alert('Geocode was not successful for the following reason: ' + status); |
| 51 | + } |
| 52 | + }); |
| 53 | +
|
| 54 | + } |
| 55 | + } |
| 56 | +
|
| 57 | + function showMap(){ |
| 58 | + latlng = new google.maps.LatLng((location1.lat()+location2.lat())/2,(location1.lng()+location2.lng())/2); |
| 59 | + var maptype = document.getElementById('maptype').value; |
| 60 | + var typeId; |
| 61 | +
|
| 62 | + if (maptype == 'roadmap') |
| 63 | + typeId = google.maps.MapTypeId.ROADMAP; |
| 64 | + else if (maptype == 'hybrid') |
| 65 | + typeId = google.maps.MapTypeId.HYBRID; |
| 66 | + else if (maptype == 'satellite') |
| 67 | + typeId = google.maps.MapTypeId.SATELLITE; |
| 68 | + else if (maptype == 'terrain') |
| 69 | + typeId = google.maps.MapTypeId.TERRAIN; |
| 70 | +
|
| 71 | + var mapOptions = { |
| 72 | + zoom: 1, |
| 73 | + center: latlng, |
| 74 | + mapTypeId: typeId |
| 75 | + }; |
| 76 | +
|
| 77 | + map = new google.maps.Map(document.getElementById(\"map_canvas\"), mapOptions); |
| 78 | +
|
| 79 | + google.maps.event.addListener(map, 'maptypeid_changed', function() { |
| 80 | + maptype = map.getMapTypeId(); |
| 81 | + document.getElementById('maptype').value = maptype; |
| 82 | + }); |
| 83 | +
|
| 84 | + var rabbit = new google.maps.MarkerImage('distance-finder-custom-marker-image.png'); |
| 85 | +
|
| 86 | + var marker1 = new google.maps.Marker({ |
| 87 | + map: map, |
| 88 | + position: location1, |
| 89 | + title: 'First location', |
| 90 | + icon: rabbit, |
| 91 | + draggable: true |
| 92 | + }); |
| 93 | +
|
| 94 | + var marker2 = new google.maps.Marker({ |
| 95 | + map: map, |
| 96 | + position: location2, |
| 97 | + title: 'Second location', |
| 98 | + icon: rabbit, |
| 99 | + draggable: true |
| 100 | + }); |
| 101 | +
|
| 102 | + var text1 = '<div id=\"content\">'+ |
| 103 | + '<h1 id=\"firstHeading\">First location</h1>'+ |
| 104 | + '<div id=\"bodyContent\">'+ |
| 105 | + '<p>Coordinates: '+location1+'</p>'+ |
| 106 | + '<p>Address: '+address1+'</p>'+ |
| 107 | + '</div>'+ |
| 108 | + '</div>'; |
| 109 | +
|
| 110 | + var text2 = '<div id=\"content\">'+ |
| 111 | + '<h1 id=\"firstHeading\">Second location</h1>'+ |
| 112 | + '<div id=\"bodyContent\">'+ |
| 113 | + '<p>Coordinates: '+location2+'</p>'+ |
| 114 | + '<p>Address: '+address2+'</p>'+ |
| 115 | + '</div>'+ |
| 116 | + '</div>'; |
| 117 | +
|
| 118 | + infowindow1 = new google.maps.InfoWindow({ |
| 119 | + content: text1 |
| 120 | + }); |
| 121 | + infowindow2 = new google.maps.InfoWindow({ |
| 122 | + content: text2 |
| 123 | + }); |
| 124 | +
|
| 125 | + google.maps.event.addListener(marker1, 'click', function() { |
| 126 | + infowindow1.open(map,marker1); |
| 127 | + }); |
| 128 | + google.maps.event.addListener(marker2, 'click', function() { |
| 129 | + infowindow2.open(map,marker2); |
| 130 | + }); |
| 131 | +
|
| 132 | + google.maps.event.addListener(marker1, 'dragend', function() { |
| 133 | + location1 = marker1.getPosition(); |
| 134 | + drawRoutes(location1, location2); |
| 135 | + }); |
| 136 | +
|
| 137 | + google.maps.event.addListener(marker2, 'dragend', function() { |
| 138 | + location2 = marker2.getPosition(); |
| 139 | + drawRoutes(location1, location2); |
| 140 | + }); |
| 141 | +
|
| 142 | + directionsService = new google.maps.DirectionsService(); |
| 143 | + directionsDisplay = new google.maps.DirectionsRenderer({ |
| 144 | + suppressMarkers: true, |
| 145 | + suppressInfoWindows: true |
| 146 | + }); |
| 147 | +
|
| 148 | + directionsDisplay.setMap(map); |
| 149 | +
|
| 150 | + drawRoutes(location1, location2); |
| 151 | + } |
| 152 | +
|
| 153 | + function drawRoutes(location1, location2){ |
| 154 | +
|
| 155 | + geocoder = new google.maps.Geocoder(); |
| 156 | + if (geocoder){ |
| 157 | + geocoder.geocode({'latLng': location1}, function(results, status){ |
| 158 | + if (status == google.maps.GeocoderStatus.OK){ |
| 159 | + if (results[0]){ |
| 160 | + address1 = results[0].formatted_address; |
| 161 | + document.getElementById(\"address1\").value = address1; |
| 162 | + } |
| 163 | + } else { |
| 164 | + alert(\"Geocoder failed due to: \" + status); |
| 165 | + } |
| 166 | + }); |
| 167 | + } |
| 168 | +
|
| 169 | + if (geocoder){ |
| 170 | + geocoder.geocode({'latLng': location2}, function(results, status){ |
| 171 | + if (status == google.maps.GeocoderStatus.OK){ |
| 172 | + if (results[0]){ |
| 173 | + address2 = results[0].formatted_address; |
| 174 | + document.getElementById(\"address2\").value = address2; |
| 175 | + continueShowRoute(location1, location2); |
| 176 | + } |
| 177 | + } else { |
| 178 | + alert(\"Geocoder failed due to: \" + status); |
| 179 | + } |
| 180 | + }); |
| 181 | + } |
| 182 | + } |
| 183 | +
|
| 184 | + function continueShowRoute(location1, location2){ |
| 185 | + if (line){ |
| 186 | + line.setMap(null); |
| 187 | + } |
| 188 | +
|
| 189 | + line = new google.maps.Polyline({ |
| 190 | + map: map, |
| 191 | + path: [location1, location2], |
| 192 | + strokeWeight: 7, |
| 193 | + strokeOpacity: 0.8, |
| 194 | + strokeColor: '#FFAA00' |
| 195 | + }); |
| 196 | +
|
| 197 | + var R = 6371; |
| 198 | + var dLat = toRad(location2.lat()-location1.lat()); |
| 199 | + var dLon = toRad(location2.lng()-location1.lng()); |
| 200 | +
|
| 201 | + var dLat1 = toRad(location1.lat()); |
| 202 | + var dLat2 = toRad(location2.lat()); |
| 203 | +
|
| 204 | + var a = Math.sin(dLat/2) * Math.sin(dLat/2) + |
| 205 | + Math.cos(dLat1) * Math.cos(dLat1) * |
| 206 | + Math.sin(dLon/2) * Math.sin(dLon/2); |
| 207 | + var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); |
| 208 | + var d = R * c; |
| 209 | +
|
| 210 | + document.getElementById(\"distance_direct\").innerHTML = \"<br/>Distanza tra i due punti (in linea d'aria): \"+d; |
| 211 | +
|
| 212 | + var travelmode = document.getElementById(\"travelMode\").value; |
| 213 | +
|
| 214 | + if (travelmode == \"driving\") |
| 215 | + travel = google.maps.DirectionsTravelMode.DRIVING; |
| 216 | + else if (travelmode == \"walking\") |
| 217 | + travel = google.maps.DirectionsTravelMode.WALKING; |
| 218 | + else if (travelmode == \"bicycling\") |
| 219 | + travel = google.maps.DirectionsTravelMode.BICYCLING; |
| 220 | +
|
| 221 | + var request = { |
| 222 | + origin:location1, |
| 223 | + destination:location2, |
| 224 | + travelMode: travel |
| 225 | + }; |
| 226 | + directionsService.route(request, function(response, status){ |
| 227 | + if (status == google.maps.DirectionsStatus.OK){ |
| 228 | + var price = response.routes[0].legs[0].distance.value /1000 * $costokm + 5.0; |
| 229 | + directionsDisplay.setDirections(response); |
| 230 | + distance = 'Distanza tra i due punti scelti sul percorso: '+response.routes[0].legs[0].distance.text; |
| 231 | + distance += '<br/>Tempo approssimativo '+travelmode+': '+response.routes[0].legs[0].duration.text; |
| 232 | + distance += '<br/>Costo approssimativo in euro: '+price; |
| 233 | + document.getElementById('distance_road').innerHTML = distance; |
| 234 | + } else { |
| 235 | + alert('error: ' + status); |
| 236 | + } |
| 237 | + }); |
| 238 | +
|
| 239 | + var text1 = '<div id=\"content\">'+ |
| 240 | + '<h1 id=\"firstHeading\">First location</h1>'+ |
| 241 | + '<div id=\"bodyContent\">'+ |
| 242 | + '<p>Coordinates: '+location1+'</p>'+ |
| 243 | + '<p>Address: '+address1+'</p>'+ |
| 244 | + '</div>'+ |
| 245 | + '</div>'; |
| 246 | +
|
| 247 | + var text2 = '<div id=\"content\">'+ |
| 248 | + '<h1 id=\"firstHeading\">Second location</h1>'+ |
| 249 | + '<div id=\"bodyContent\">'+ |
| 250 | + '<p>Coordinates: '+location2+'</p>'+ |
| 251 | + '<p>Address: '+address2+'</p>'+ |
| 252 | + '</div>'+ |
| 253 | + '</div>'; |
| 254 | +
|
| 255 | + infowindow1.setContent(text1); |
| 256 | + infowindow2.setContent(text2); |
| 257 | + } |
| 258 | +
|
| 259 | + function toRad(deg){ |
| 260 | + return deg * Math.PI/180; |
| 261 | + } |
| 262 | + "); |
| 263 | + |
| 264 | +$document->addScriptDeclaration(" |
| 265 | +
|
| 266 | +"); |
| 267 | + |
| 268 | +//$pflip = modPflipHelper::myfunction(); |
| 269 | + |
| 270 | +require JModuleHelper::getLayoutPath('mod_taxi', $params->get('layout', 'default')); |
| 271 | + |
0 commit comments