do geocoding before form submit - issue

582 views
Skip to first unread message

fajnalowiec

unread,
Apr 11, 2011, 6:12:14 AM4/11/11
to google-map...@googlegroups.com
I made a simple html form based on google tutorial example. It is in the attachment of this post. All I want is to submit my form, but before this I want to get geocoder location (lat, lng) and put them in the hidden fields of my form, so I could use them later in my php script via $_POST. So far, it works only if I first choose to geocode, then submit. Any solution would be appreciated.
google_map.php

en4ce

unread,
Apr 11, 2011, 6:58:10 AM4/11/11
to Google Maps JavaScript API v3
1. change the submit button to a regular one and attach a send
function via onclick
2. call the same function in the call back of your codeAddress() so
after: if (status == google.maps.GeocoderStatus.OK) {.....}
3. submit function could look like this:

function sendMyForm(){
document.myform.submit();
>  google_map.php
> 1KAnzeigenHerunterladen

fajnalowiec

unread,
Apr 11, 2011, 8:07:34 AM4/11/11
to google-map...@googlegroups.com
I must say big "WOW". I am really, really impressed. Thanks a lot :D I spent a few hours fruitlessly looking for solutions and it was so close.
For future JS development I have one more question;

with a code like this:

function f() {
  ...
  geocoder.geocode(...
  if (status == google.maps.GeocoderStatus.OK) {
    //some stuff
  }
);
document.myform.submit();
}

why submit of the form is done and execution of "//some stuff" is omited? AFAIU, in this case the form submit is done earlier. Why JS doesn' t wait for finish method geocode()?

geoco...@gmail.com

unread,
Apr 11, 2011, 8:29:10 AM4/11/11
to Google Maps JavaScript API v3
Geocoding is asynchronous. It sends a request off to the server,
continuing execution of your code (not waiting), when the response
comes back containing the data, the callback routine runs (the "some
stuff"), but by then your submit has already been executed.

You need to put any code that depends on the returned data _inside_
the callback routine.

-- Larry

fajnalowiec

unread,
Apr 11, 2011, 11:21:13 AM4/11/11
to Google Maps JavaScript API v3
On Apr 11, 2:29 pm, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:

> Geocoding is asynchronous.  It sends a request off to the server,
> continuing execution of your code (not waiting), when the response
> comes back containing the data, the callback routine runs (the "some
> stuff"), but by then your submit has already been executed.
>
> You need to put any code that depends on the returned data _inside_
> the callback routine.

now, everything become so simple. thanks for Your explanation. :)

best regards,
artur.

en4ce

unread,
Apr 11, 2011, 1:00:20 PM4/11/11
to Google Maps JavaScript API v3
your welcome =)
Reply all
Reply to author
Forward
0 new messages