2012-04-21 16 views
5

Desidero ottenere l'attuale posizione GPS dal dispositivo dell'utente e in pratica trascinare Lat e Long nei campi modulo. Quindi posso fare qualsiasi cosa dopo aver inserito la posizione.Ingresso modulo JavaScript - GPS - GeoLocation

Il codice seguente funziona per una posizione di avviso pop-up, che è quello che ho iniziato. Voglio essere in grado di fare clic su un pulsante e farlo compilare il long/lat nei campi del modulo.

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

risposta

6

Un mio amico mi ha aiutato ... Questo funziona 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

c'è qualche possibilità che ci dica su quali dispositivi funziona? –

+1

Funziona su tablet Android (Galaxy) - Samsung Galaxy II, iPhone, iPad, iPhone ... I servizi di localizzazione devono essere attivi su iPhone/iPad –

0

Il codice fornito da @Merle_The_Pearl funziona in Safari e Firefox su OSX e con la notevole eccezione che Firefox non lo fa non chiamare "onGeoError" se l'utente decide di non fornire la loro posizione. Questa sembra essere una sfortunata decisione politica del popolo Mozilla di ignorare la specifica W3C https://bugzilla.mozilla.org/show_bug.cgi?id=675533

Problemi correlati