SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry GeoCoding UK Postcodes NEED HELP!!! Wierd Behaviour...

    OK, here goes, im new so bear with me...
    I have a form on which the user will fill in their details which includes their postcode.
    usercontrol ascx Postcode, Hidden and Button Fields are as follows:

    <asp:TextBox ID="txtPostcode" runat="server""></asp:TextBox>
    <asp:HiddenField ID="OrgLat" runat="server" />
    <asp:HiddenField ID="OrgLon" runat="server" />
    <asp:Button ID="btnPostJob" runat="server" Text="Submit" OnClientClick="SetHidFields()" />

    JavaScript as follows:

    function SetHidFields()
    {
    var pcode = document.getElementById('ctl09_txtPostcode').value;
    alert("postcode: " + pcode);
    usePointFromPostcode1(pcode,setHidVals)
    alert("ANNOYING!");
    }

    function usePointFromPostcode1(postcode1, callbackFunction)
    {
    localSearch.setSearchCompleteCallback(null,
    function()
    {
    if (localSearch.results[0])
    {
    var resultLat = localSearch.results[0].lat;
    var resultLng = localSearch.results[0].lng;
    var point = new GLatLng(resultLat,resultLng); callbackFunction(point);
    }
    else
    {
    alert("Your Postcode Was Not Found!");
    }
    });
    localSearch.execute(postcode1 + ", UK");
    }

    function setHidVals(point)
    {
    alert('lat:' + point.lat() + ' lon:' + point.lng());
    document.getElementById('ctl09_OrgLat').value = point.lat();
    document.getElementById('ctl09_OrgLon').value = point.lng();
    }

    Now I have code on the server side handling the button click. Basically it needs the values in the two hidden fields i.e. latitude and longitude. Ive done LOADS of tinkering with this e.g.
    if I use a html button and its onclick event, lat and long values are no problem, then how will the server side code be executed?

    Theres one aspect that really confuses me: when the "annoying" alert in SetHidFields js exists, the hidden fields are populated and the server side code can get the values during postback, but when the alert doesnt exist, the hidden fields are never populated with values. Its like the alert holds up the process long enough to get the lat long values from google.

    Code was taken from:
    Please google: "tomanthony geocoding-uk-postcodes-with-google-map"

    I see he used a html input and the onclick event, which works no problem, but trying to get this working flawlessly with a button server control and postback is proving difficult for me.

    Please Help, i'm going crazy with this!

  2. #2
    SitePoint Enthusiast Chris Webb's Avatar
    Join Date
    Oct 2008
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the problem is your usePointFromPostcode1 function runs an asynchonous request to Google, which the form won't wait for completion before submitting (it obviously completes while you deal with the alert() popup).

    Two suggestions:

    1) Run the usePointFromPostcode1 on a onchange event on the textbox, on the assumption it only runs if a valid postcode is entered (otherwise you'll generate too many requests to google)

    2) change your onclientclick code to:

    OnClientClick="return SetHidFields()"

    then replace your alert with something like:

    while(document.getElementById('ctl09_OrgLat').value == ""){
    continue;
    }
    return true;

    which should hopefully pause the function until the response is set on the hidden input, then allow the form to be posted.
    Chris Webb | twitter/tgnwg
    Visit TheGoodNamesWereGone.com for
    free Javascript goodness, help with your Website and more


  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Good Man Chris,

    Did the onkeyup on the textbox. Problem sorted. What a difference a good nights sleep does.

    Thanks

    EDIT: oh yeah had to validate the postcode, i.e. check for all formats first before allowing a request to google. Tip for anyone: strip out all spaces before such validation, it keeps things simple.

  4. #4
    SitePoint Enthusiast Chris Webb's Avatar
    Join Date
    Oct 2008
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No worries ;-)
    Chris Webb | twitter/tgnwg
    Visit TheGoodNamesWereGone.com for
    free Javascript goodness, help with your Website and more



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •