2012-04-21 14 views
5

私は現在のGPS位置をユーザーのデバイスから取得したいと考えており、基本的にLatとLongをフォームフィールドにドロップします。それから私は場所を挿入した後に何でもすることができます。Javascriptフォーム入力 - GPS - GeoLocation

以下のコードは、私が始めたポップアップアラートの場所で動作します。私は、ボタンをクリックして、長い/緯度をフォームフィールドに入力させたいと思っています。

<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

私の友人が私を助けて... これは@Merle_The_Pearlによって提供されたコードは、Firefoxがないことを注目すべき例外を除いて、同様OSX上のSafariとFirefoxで動作し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

このデバイスがどのデバイスで動作するか教えてください。 –

+1

Androidタブレット(ギャラクシー)で動作 - Samsung Galaxy II、iPhone、iPad、iPhone ... iPhone/iPad用ロケーションサービスが必要 –

0

の作品ユーザーが自分の場所を指定しないと決めた場合は、 "onGeoError"を呼び出さないでください。これはMozillaの人々がW3C仕様を無視するという不幸な政策決定であると思われますhttps://bugzilla.mozilla.org/show_bug.cgi?id=675533

関連する問題