I would like to use javascript to log Visitor's exact GEO location into a txt file (just cordination is OK). Any Ideas?
Asked
Active
Viewed 683 times
0
-
Which language do you use for `backend` ? – SayJeyHi May 11 '18 at 07:11
-
JavaScript does not have access to filesystem, so it cannot do any write operations to disk. – A G May 11 '18 at 07:12
-
Im using PHP for the backend sir – May 11 '18 at 07:13
-
@NguyễnNgọcAnh see my answer update – SayJeyHi May 11 '18 at 07:21
2 Answers
0
You could get user latitude
and longitude
with HTML5
getolocation and then send it with ajax
to your save location api
.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function initGeolocation()
{
if( navigator.geolocation )
{
// Call getCurrentPosition with success and failure callbacks
navigator.geolocation.getCurrentPosition( success, fail );
}
else
{
alert("Sorry, your browser does not support geolocation services.");
}
}
function success(position)
{
document.getElementById('long').value = position.coords.longitude;
document.getElementById('lat').value = position.coords.latitude
}
function fail()
{
// Could not obtain location
}
var $locationForm = $("#LocationSaverForm");
$locationForm.on("submit" , function(){
var data = $(this)[0].serailize();
$.ajax({
type: "POST",
url: "locationSaver.php",
data: data,
});
});
</script>
</head>
<body onLoad="initGeolocation();">
<FORM NAME="rd" id="LocationSaverForm" METHOD="POST" ACTION="index.html">
<INPUT TYPE="hidden" NAME="long" ID="long" VALUE="">
<INPUT TYPE="hidden" NAME="lat" ID="lat" VALUE="">
</FORM>
</body>
</html>
then submit form and get information in backend.

SayJeyHi
- 1,559
- 4
- 19
- 39
0
Geolocation.getCurrentPosition() can give the location co-ordinates.
https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition
here is a StackOverflow post to write data on a file using nodeJS fs module: Writing files in Node.js
combining Geolocation.getCurrentPosition() and fs module coordinates can be saved on a text file.

Bhavya Bharti
- 121
- 5