OK, I figured the solution out myself. The above code only populates the text field. If you don't move the marker anymore after doing so, you need to write the values to the database as well.
Specifically this line is missing:
Code:
$('#answer{SGQ}').val(Math.round(results[0].geometry.location.lat()*10000)/10000 + ';' + Math.round(results[0].geometry.location.lng()*10000)/10000);
See full and working code example here. With the new {SGQ} identifier, you could shorten this code even more.
Code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Identify the elements
var mapQ = $('#question{QID}');
var mapInput = $('input.text.location', mapQ);
var mapSGQA = mapInput.attr('id').replace(/answer/, '').replace(/_c/, '');
var map = gmaps[mapSGQA+'_c'];
var marker = gmaps['marker__'+mapSGQA+'_c'];
var addressQ = mapQ.prevAll('.text-short:eq(0)');
var addressInput = $('input.text', addressQ);
// Wait for the maps to load
google.maps.event.addListenerOnce(map, 'idle', function(){
// Initialize geocoder
var geocoder = new google.maps.Geocoder();
// Geocode look-up function
function codeAddress() {
var address = "{PERSDATA_PD002} Steyregg";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
// Move the map and marker
map.setCenter(results[0].geometry.location);
marker.setPosition(results[0].geometry.location);
map.panTo(marker.getPosition())
// Record the new position
mapInput.val(Math.round(results[0].geometry.location.lat()*10000)/10000 + " " + Math.round(results[0].geometry.location.lng()*10000)/10000);
$('#answer{SGQ}').val(Math.round(results[0].geometry.location.lat()*10000)/10000 + ';' + Math.round(results[0].geometry.location.lng()*10000)/10000);
}
else {
alert("Automatische Adresssuche nicht erfolgreich. Fehler: " + status);
}
});
}
codeAddress();
});
});
</script>