mardi 14 avril 2015

Google map geocoding with form submit

Problem: I have a form with some input fields, and i'm trying to convert the adress to geocode before it submits to my controller (using Spring MVC). But somehow my geolocation field is empty after the conversion... And for some reason the same code does work on a form without a submit. Can someone help me please? Thanks a lot!


Code:



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="domain.Location"%>
<%@taglib prefix="c" uri="http://ift.tt/QfKAz6" %>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Leuven Speaks</title>
<script type='text/javascript' src='http://ift.tt/ruu4ao'></script>
<script type="text/javascript" src="http://ift.tt/1GIjxw9"></script>
</head>
<body>
<jsp:include page="header.jspf"/>
<script type="text/javascript" src="<c:url value="/js/style.js" />"></script>

<form name="addLocation" method="POST" action="addLocation.htm" commandName="location" onsubmit="changeAdress()">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>Adres</td>
<td><input type="text" id="adress" name="adress"/></td>
</tr>
<tr>
<td><input type="text" id="geolocation" name="geolocation"/></td>
</tr>
</table>
<input type="hidden" name="id" value="${location.id}"/>
<input type="submit" name="action" value="Save"/>
<input type="submit" name="action" value="Cancel"/>
</form>
</body>

<script type="text/javascript">
function changeAdress(){
var adres = document.getElementById("adress").value;
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': adres}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
document.addLocation.geolocation.value = latitude + " " + longitude;
}
});
//geolocation is empty...
alert(document.getElementById("geolocation").value);
return true;
}
</script>
</html>

Aucun commentaire:

Enregistrer un commentaire