2012-04-21 55 views
5

我想從用戶的設備的當前GPS位置,基本上下降緯度和長成表單字段。然後我可以在插入位置後做任何事情。JavaScript表單輸入 - GPS - GeoLocation中

下面的代碼適用於一個彈出警報的位置,這是我開始的。我希望能夠點擊一個按鈕,並將它填入表單字段中的long/lat。

<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> 

回答

6

朋友我的幫助我...... 這工作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> 
+0

有沒有可能告訴我們在哪些設備上工作? – 2012-04-22 15:20:18

+1

適用於Android平板電腦(Galaxy) - 三星Galaxy II,iPhone,iPad,iPhone ... iPhone/iPad需要位置服務 – 2012-04-22 16:56:55

0

由@Merle_The_Pearl提供的代碼在Safari和Firefox上OSX以及與顯着的例外是Firefox不如果用戶決定不提供他們的位置,則不要調用「onGeoError」。這似乎是Mozilla人忽視W3C規範的一個不幸政策決定https://bugzilla.mozilla.org/show_bug.cgi?id=675533