Page:12

# TOPIC: Ability to add Google Maps as a question

## Ability to add Google Maps as a question 1 year 9 months ago #104986

• tpartner
• LimeSurvey Team
• Posts: 5276
• Thank you received: 1101
• Karma: 458
Well, I didn't have much time to spend on this but here's a quick-and-dirty example of how to calculate the distance and travel time from the "Default position" using the Google Maps API v3 Distance matrix.

Place this in the question source.
```<script type="text/javascript" charset="utf-8">

// Identify the map
var mapSGQA = '{SGQ}';
var currentMap = gmaps[''+mapSGQA+'_c'];

// Wait for the map to load

// Some variable definitions
var currentMarker = gmaps['marker__'+mapSGQA+'_c'];
var defaultPosition = \$(answerInput).val();
var startLat = \$('#answer'+mapSGQA+'_c').val().split(' ')[0];
var startLng = \$('#answer'+mapSGQA+'_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
calculateDistances(startLatLng, currentMarker.getPosition());
});
calculateDistances(startLatLng, currentMarker.getPosition());
});
calculateDistances(startLatLng, currentMarker.getPosition());
});

// Insert the results element

});
});

function calculateDistances(origin, destination) {
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: [origin],
destinations: [destination],
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>```

Demo survey:

#### File Attachment:

File Name: travel_dis...e_v2.lss
File Size:15 KB

.

## Ability to add Google Maps as a question 1 year 9 months ago #105001

 dweisser Offline Gold Lime Posts: 206 Thank you received: 5 Karma: 5 Hey T, This is great. I think there are a lot of directions one could go here. I was working on this, but instead of using the short-text Map-style question, I ended up showing a map inside a short-text text-style question. It is not optimal because the style requirements were pretty hard to work with, however I was able to save the calculated distance as the question's answer. I assume that this is what this does:```// Insert the results element \$(answerInput).after('
');``` I suppose the "benefit" of using the external map rather than the short-text map-style question is that it might be more easy to implement driving directions, location search and things of that nature. Or is it? Your solution is really great. Thank you as always for relentlessly adding to the knowledge base. David The administrator has disabled public write access.

## Ability to add Google Maps as a question 1 year 9 months ago #105002

 tpartner Offline LimeSurvey Team Posts: 5276 Thank you received: 1101 Karma: 458 Yes, of course, this could be applied to any map. I just used the LimeSurvey map because it was faster. . Cheers, Tony Partner Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise. LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project. The administrator has disabled public write access.

## Ability to add Google Maps as a question 1 year 3 months ago #111398

 AudioMan612 Offline Fresh Lemon Posts: 4 Karma: 0 Hi, As some people have mentioned here, I would like a way to implement a search function in Google Maps in a survey. I have very limited experience with web development, so I'm struggling a bit with this (I'm used to writing stand-alone programs, not scripts for webpages). Anyways, I found a LimeSurvey that has successfully implemented a search function and I was wondering if anyone might have any clues as to how to implement this. bart.etcsurvey.com/index.php/ The first question with Google Maps is somewhere around 7 questions in. The survey remembers your choice as the origin of you trip and shows it on a map later on. I'm currently going through the sources for the webpage trying to see if I get any ideas, but any help would be awesome. Thanks! The administrator has disabled public write access.

## Ability to add Google Maps as a question 1 year 1 month ago #113715

 s9521070 Offline Fresh Lemon Posts: 1 Karma: 0 Did anyone find a way to include a "Place search box" inside a question that uses Google Maps? It would be really helpful if the functionality shown here can be used to obtain the latitude-longitude values from a Google Maps Limesurvey question without the necessity to move around the map manually. The administrator has disabled public write access.
 Page:12
Moderators: ITEd
Time to create page: 0.158 seconds