Reputation: 1521
I want to get the current GPS Location from the user's device, and basically drop Lat and Long into form fields. Then I can do whatever after inserting the location.
The code below works for a pop up alert location, which is what I started with. I want to be able to click a button and have it populate the long/lat into the form fields.
<html>
<head>
<script type="text/javascript">
function getLocationConstant()
{
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoFormLat,on GeoFormLong,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>
Upvotes: 7
Views: 7657
Reputation: 381
The code provided by @Merle_The_Pearl works in Safari and Firefox on OSX as well with the notable exception that Firefox does not not call the "onGeoError" if the user decides not to provide their location. This appears to be an unfortunate policy decision by the Mozilla people to ignore the W3C spec https://bugzilla.mozilla.org/show_bug.cgi?id=675533
Upvotes: 0
Reputation: 1521
Friend of mine helped me out... This works gr8
<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>
<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>
Upvotes: 6