Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple JS problem: GPS - Form

    Looking for help with a quick JS project.

    I need this (or similar) code to pull the GPS info from a mobile browser and update two hidden form fields with the GPS coordinates. If the function could loop and continue to watchPosition() and update the form without interrupting the end user's attempt to fill out the rest ofthe form, that would be awesome.

    This is in the HEAD:

    Code:
    function getLocationConstant() { 
    if(navigator.geolocation) 
    { navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError); } 
    else 
    { alert("Your browser or device doesn't support Geolocation"); } 
    } 
    
    // If we have a successful location update 
    function onGeoSuccess(event) { 
    document.getElementById("Latitude").value =  event.coords.latitude; 
    document.getElementById("Longitude").value = event.coords.longitude; 
    } 
    
    // If something has gone wrong with the geolocation request 
    function onGeoError(event) { 
    alert("Error code " + event.code + ". " + event.message); 
    }

    This is in the FORM:

    Code:
    <img src="spacer.gif" OnLoad="getLocationConstant()">
    <input type="Hidden" name="Latitude" id="Latitude" value="">
    <input type="Hidden" name="Longitude" id="Longitude" value="">

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    i think all you need to do is swap your call:

    changing
    Code:
    navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError);
    to
    Code:
    navigator.geolocation.watchPosition(onGeoSuccess,onGeoError);
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #3
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, but my main issue is that the JS isn't updating the form fields.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,965
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Does the browser prompt for permission to get your location? When you allow it, does it execute the onGeoSuccess handler? Or does it go to onGeoError handler?


  •  

    Posting Permissions

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