- Posts: 2
- Thank you received: 0
Ask the community, share ideas, and connect with other LimeSurvey users!
<script type="text/javascript" charset="utf-8"> $(document).ready(function(){ // Identify the map var map2SGQA = '{SGQ}'; var map1SGQA = '774328X12X32'; var currentMap = gmaps[''+map2SGQA+'_c']; // Wait for the map to load google.maps.event.addListenerOnce(currentMap, 'idle', function(){ // Some variable definitions var currentMarker = gmaps['marker__'+map2SGQA+'_c']; var answerInput = $('#answer'+map2SGQA+'_c'); var defaultPosition = $(answerInput).val(); var startLat = $('{INSERTANS:'+map1SGQA+'}').val().split(';')[0]; var startLng = $('{INSERTANS:'+map1SGQA+'}').val().split(';')[1]; var startLatLng = new google.maps.LatLng(startLat, startLng); var originIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000'; // Listener on the map events google.maps.event.addListener(currentMap, 'click', function() { calculateDistances(startLatLng, currentMarker.getPosition()); }); google.maps.event.addListener(currentMarker, 'dragend', function() { calculateDistances(startLatLng, currentMarker.getPosition()); }); google.maps.event.addListener(currentMap, 'rightclick', function() { calculateDistances(startLatLng, currentMarker.getPosition()); }); // Insert the results element $(answerInput).after('<div class="distanceResults" />'); }); }); function calculateDistances(origin, destination) { var service = new google.maps.DistanceMatrixService(); service.getDistanceMatrix({ origins: [origin], destinations: [destination], travelMode: google.maps.TravelMode.DRIVING, unitSystem: google.maps.UnitSystem.METRIC, avoidHighways: false, avoidTolls: false }, callback); } function callback(response, status) { if (status != google.maps.DistanceMatrixStatus.OK) { alert('Error was: ' + status); } else { var origins = response.originAddresses; var destinations = response.destinationAddresses; var outputDiv = $('.questiontext'); outputDiv.innerHTML = ''; for (var i = 0; i < origins.length; i++) { var results = response.rows[i].elements; for (var j = 0; j < results.length; j++) { $('.distanceResults').html('Start address: '+origins[i]+'<br />\ End address: '+destinations[j]+'<br />\ Distance: '+results[j].distance.text+'<br />\ Time: '+results[j].duration.text+''); } } } } </script>
<script type="text/javascript" charset="utf-8"> $(document).ready(function(){ // Identify the map var map2SGQA = '{SGQ}'; var map1SGQA = '774328X12X32'; var currentMap = gmaps[''+map2SGQA+'_c']; var prevMap = gmaps[''+map1SGQA+'_c']; // Wait for the map to load google.maps.event.addListenerOnce(currentMap, 'idle', function(){ // Some variable definitions var currentMarker = gmaps['marker__'+map2SGQA+'_c']; var prevMarker = gmaps['marker__'+map1SGQA+'_c']; var answerInput = $('#answer'+map2SGQA+'_c'); var defaultPosition = $(answerInput).val(); var startLat = $('#answer'+map1SGQA+'_c').val().split(' ')[0]; var startLng = $('#answer'+map1SGQA+'_c').val().split(' ')[1]; var startLatLng = new google.maps.LatLng(startLat, startLng); var originIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000'; // Listener on the map events google.maps.event.addListener(prevMap, 'click', function() { startLatLng = prevMarker.getPosition(); }); google.maps.event.addListener(prevMarker, 'dragend', function() { startLatLng = prevMarker.getPosition(); }); google.maps.event.addListener(prevMap, 'rightclick', function() { startLatLng = prevMarker.getPosition(); }); google.maps.event.addListener(currentMap, 'click', function() { calculateDistances(startLatLng, currentMarker.getPosition()); }); google.maps.event.addListener(currentMarker, 'dragend', function() { calculateDistances(startLatLng, currentMarker.getPosition()); }); google.maps.event.addListener(currentMap, 'rightclick', function() { calculateDistances(startLatLng, currentMarker.getPosition()); }); // Insert the results element $(answerInput).after('<div class="distanceResults" />'); }); }); function calculateDistances(origin, destination) { var service = new google.maps.DistanceMatrixService(); service.getDistanceMatrix({ origins: [origin], destinations: [destination], travelMode: google.maps.TravelMode.DRIVING, unitSystem: google.maps.UnitSystem.METRIC, avoidHighways: false, avoidTolls: false }, callback); } function callback(response, status) { if (status != google.maps.DistanceMatrixStatus.OK) { alert('Error was: ' + status); } else { var origins = response.originAddresses; var destinations = response.destinationAddresses; var outputDiv = $('.questiontext'); outputDiv.innerHTML = ''; for (var i = 0; i < origins.length; i++) { var results = response.rows[i].elements; for (var j = 0; j < results.length; j++) { $('.distanceResults').html('Start address: '+origins[i]+'<br />\ End address: '+destinations[j]+'<br />\ Distance: '+results[j].distance.text+'<br />\ Time: '+results[j].duration.text+''); } } } } </script>
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify the elements var map1Question = $('input.text.location:eq(0)').closest('.text-short'); var map2Question = $('input.text.location:eq(1)').closest('.text-short'); var map1SGQA = $('input.text.location', map1Question).attr('id').replace(/answer/, '').replace(/_c/, ''); var map1 = gmaps[''+map1SGQA+'_c']; var marker1 = gmaps['marker__'+map1SGQA+'_c']; var map1Input = $('#answer'+map1SGQA+'_c'); var map2SGQA = $('input.text.location', map2Question).attr('id').replace(/answer/, '').replace(/_c/, ''); var map2 = gmaps[''+map2SGQA+'_c']; var marker2 = gmaps['marker__'+map2SGQA+'_c']; var map2Input = $('#answer'+map2SGQA+'_c'); var resultsInput = $('.text-long:eq(0) .textarea'); // Disable the results textarea $(resultsInput).attr('readonly', true); // Wait for the maps to load google.maps.event.addListenerOnce(map1, 'idle', function(){ // Listeners on the map 1 events google.maps.event.addListener(map1, 'click', function() { updateDistance(); }); google.maps.event.addListener(marker1, 'dragend', function() { updateDistance(); }); google.maps.event.addListener(map1, 'rightclick', function() { updateDistance(); }); }); google.maps.event.addListenerOnce(map2, 'idle', function(){ // Listeners on the map 2 events google.maps.event.addListener(map2, 'click', function() { updateDistance(); }); google.maps.event.addListener(marker2, 'dragend', function() { updateDistance(); }); google.maps.event.addListener(map2, 'rightclick', function() { updateDistance(); }); }); function updateDistance() { var startLat = $(map1Input).val().split(' ')[0]; var startLng = $(map1Input).val().split(' ')[1]; var startLatLng = new google.maps.LatLng(startLat, startLng); var endLat = $(map2Input).val().split(' ')[0]; var endLng = $(map2Input).val().split(' ')[1]; var endLatLng = new google.maps.LatLng(endLat, endLng); calculateDistances(startLatLng, endLatLng); } function calculateDistances(origin, destination) { var service = new google.maps.DistanceMatrixService(); service.getDistanceMatrix({ origins: [origin], destinations: [destination], travelMode: google.maps.TravelMode.DRIVING, unitSystem: google.maps.UnitSystem.METRIC, avoidHighways: false, avoidTolls: false }, callback); } function callback(response, status) { if (status != google.maps.DistanceMatrixStatus.OK) { alert('Error was: ' + status); } else { var origins = response.originAddresses; var destinations = response.destinationAddresses; for (var i = 0; i < origins.length; i++) { var results = response.rows[i].elements; for (var j = 0; j < results.length; j++) { $(resultsInput).val('Start address: '+origins[i]+'\n\nEnd address: '+destinations[j]+'\n\nDistance: '+results[j].distance.text+'\n\nTime: '+results[j].duration.text+''); } } } } }); </script>
google.maps.event.addListener(map1, 'center_changed', function() { updateDistance(); });
<script> var geocoder1; var mapgeo1; var markergeo1; function initialize1() { geocoder1 = new google.maps.Geocoder(); mapgeo1 = gmaps[''+'{SGQ}_c']; markergeo1 = gmaps['marker__{SGQ}_c']; } function codeAddress1() { var address1 = document.getElementById('address1').value; geocoder1.geocode( { 'address': address1}, function(results1, status1) { if (status1 == google.maps.GeocoderStatus.OK) { var coords1 = String(results1[0].geometry.location) coords1 = coords1.replace(/\(?\)?\,?/g, '') document.getElementById('answer{SGQ}_c').value = coords1; mapgeo1.setCenter(results1[0].geometry.location); markergeo1.setPosition(results1[0].geometry.location); } else { alert('Geocode was not successful for the following reason: ' + status1); } }); } google.maps.event.addDomListener(window, 'load', initialize1); </script>
<div id="panel"> <input id="address1" type="textbox" value="" /> <input onclick="codeAddress1()" type="button" value="Suche" /></div>