Skip to main content
Inspiring
April 21, 2012
Answered

Javascript Form Input - GPS - GeoLocation

  • April 21, 2012
  • 2 replies
  • 3141 views

I'm stuck... Googled this to death...

Can't seem to get what I want it to do...

I'm not best with Javascript either...

I want to get GPS Location from device - and basically drop Lat and Long into form fields...

Then I can do whatever after inserting the location etc...

This code below works for a pop up alert location... Which is what I started with...

I want to be able to click button and it just populates the long/lat into the form fields...

Any help is appreciated...

Thx...

<html>
<head>

script type="text/javascript">
function getLocationConstant()
{
  if(navigator.geolocation)
  {
   navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoFormLat,onGeoFormLong,onGeoError);
  } else {
   alert("Your browser or device doesn't support Geolocation");
  }
}

// If we have a successful location update
function onGeoSuccess(event)
{
  alert(event.coords.latitude + ', ' + event.coords.longitude);
}

  function onGeoFormLat(event)
{
var myVal;
myVal = document.getElementById(event.coords.latitude).value;
}
function onGeoFormLong(event)
{
var myVal;
myVal = document.getElementById(event.coords.longitude).value;
}
// If something has gone wrong with the geolocation request
function onGeoError(event)
{
  alert("Error code " + event.code + ". " + event.message);
}

// Called when we want to stop getting location updates
function stopGetLocation(event)
{
  navigator.geolocation.clearWatch(watchID);
}

/script>


</head>
<body>
<br><br>
      Latitude: <input type="text" id="Latitude" name="onGeoFormLat" value="">
<br><br>
      Longitude: <input type="text" id="Longitude" name="onGeoFormLong" value="">
<br>
    
<br><br><br>
          <input type="button" value="Get Location" onclick="getLocationConstant()" />
<br><br>

</body>
</html>

    This topic has been closed for replies.
    Correct answer Merle_Hanson

    I thought u could use javascript with coldfusion etc...

    Perhaps we are own worst enemies at times... Worrying about only ColdFusion...

    A friend of mine helped me out with this... And solution is below...

    Works great... And can be used to push GeoLocation Data for use in ColdFusion...

    <html>
    <head>

    <script type="text/javascript">
    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);
    }
    </script>


    </head>
    <body>
    <br><br>
    <cfform action="gps2.cfm" method="post">
          Latitude: <input type="text" id="Latitude" name="Latitude" value="">
    <br><br>
          Longitude: <input type="text" id="Longitude" name="Longitude" value="">
    <br><br>
                     <input type="button" value="Get Location" onclick="getLocationConstant()"/>
      <br><br>
    <input type="submit" value="Add GPS Location" class="small">
    </cfform>
    <br><br><br>

    <br><br>

    </body>
    </html>

    2 replies

    Merle_HansonAuthorCorrect answer
    Inspiring
    April 22, 2012

    I thought u could use javascript with coldfusion etc...

    Perhaps we are own worst enemies at times... Worrying about only ColdFusion...

    A friend of mine helped me out with this... And solution is below...

    Works great... And can be used to push GeoLocation Data for use in ColdFusion...

    <html>
    <head>

    <script type="text/javascript">
    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);
    }
    </script>


    </head>
    <body>
    <br><br>
    <cfform action="gps2.cfm" method="post">
          Latitude: <input type="text" id="Latitude" name="Latitude" value="">
    <br><br>
          Longitude: <input type="text" id="Longitude" name="Longitude" value="">
    <br><br>
                     <input type="button" value="Get Location" onclick="getLocationConstant()"/>
      <br><br>
    <input type="submit" value="Add GPS Location" class="small">
    </cfform>
    <br><br><br>

    <br><br>

    </body>
    </html>

    Inspiring
    April 22, 2012

    I thought u could use javascript with coldfusion etc...

    Well of course... one can use Flash with CF as well, but one would be daft to ask a Flash question on a CF forum.

    If you want an answer to a JS question, the best place to ask it would be on a JS forum.  That's what I meant. 

    Perhaps we are own worst enemies at times... Worrying about only ColdFusion...

    That's a bit of a daft thing to say.  Just because we happen to use a bunch of different technologies in the course of arriving at a solution doesn't mean a forum for one of the technologies is the best approach to getting solutions for all of them.

    --

    Adam

    Inspiring
    April 22, 2012

    I might be missing something, but what's this got to do with CF?  It looks like pure client-side stuff to me?  Would it be better posted on a JS forum?

    --

    Adam