Hi,
I'm new to Google Maps API and don't do a lot of Javascript coding.
I'm building a web app that lets users input an address into a form.
I'm using an "onsubmit" to geocode the address and store the
structured address, lat and lng in hidden fields and then passing them
to a php script when submitted. The goal is to store the lat/lng in a
user's profile when it's created to limit the number of geocoding
calls. Another script will pull all of the users and display them on
a map.
For some reason, the app is not able to pass the geocode data back to
the hidden fields in the form unless I enable an alert in one of the
javascript functions. Here is a code snippet. Can anyone give me
some pointers on why turning on an alert in one of the functions makes
it work but otherwise the data is not stored or passed to the php
script?
HTML Form with onsubmit used for Google Map geocoding:
<html>
<head>
<title>Code Snippet - Geocoding using onsubmit in form</title>
<script src="http://maps.google.com/maps?
file=api&v=2.x&key=xxx"
type="text/javascript"></script>
<script type="text/javascript">
var geocoder;
function initialize() {
geocoder = new GClientGeocoder();
}
</script>
<script type="text/javascript">
function InsertAddressLatLng(response) {
if (!response || response.Status.code != 200) {
alert("Sorry, there was an error geocoding your address");
} else {
place = response.Placemark[0];
document.inputaddress.field_2.value =
place.Point.coordinates[1];
document.inputaddress.field_3.value =
place.Point.coordinates[0];
document.inputaddress.field_4.value = place.address;
}
}
function showLocation() {
var address = document.inputaddress.field_1.value;
geocoder.getLocations(address, InsertAddressLatLng);
//debugging alert statement- form works only when this is turned
on.
alert(address);
}
</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<form name="inputaddress" action="finished_test.php"
onsubmit="showLocation();"
method="POST">
Address: <input type="text" name="field_1" size="50">
<input type="hidden" name="field_2">
<input type="hidden" name="field_3">
<input type="hidden" name="field_4">
<input type="submit" value="Submit">
</form>
</body>
</html>
php script that displays the values passed from the form:
<html>
<head>
<title>Finished Document</title>
</head>
<body>
Field 1: <?php echo $_POST["field_1"]; ?>.<br />
Field 2: <?php echo $_POST["field_2"]; ?>.<br/>
Field 3: <?php echo $_POST["field_3"]; ?>.<br/>
Field 4: <?php echo $_POST["field_4"]; ?>.<br/>
</br/>
<a href="test_function.html">Add Another Record<br/></a>
<br/>
</body>
</html>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Maps API" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Maps-API?hl=en
-~----------~----~----~----~------~----~------~--~---