我想从用户的设备中获取当前的 GPS 位置,并且基本上将 Lat 和 Long 放入表单字段中。然后我可以在插入位置后做任何事情。
下面的代码适用于弹出警报位置,这是我开始使用的。我希望能够单击一个按钮并将其填充长/纬度到表单字段中。
<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>