简体   繁体   English

如何将地理位置输出导出到日志文件?

[英]how can i export the geolocation output to log file?

I have this code it displays the geolocation (Longitude: xx Latitude: xx Accuracy: xxx) ..how can I output the results into a log file log.txt when any body visit the url 我有这段代码,它显示地理位置(经度:xx纬度:xx精度:xxx)..当任何人访问url时,如何将结果输出到日志文件log.txt中

    <!-- <xml version="1.0" encoding="utf-8"> -->
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Geolocation API Demo</title>
<meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" name="viewport"/>
<script>
function successHandler(location) {
    var message = document.getElementById("message"), html = [];
    html.push("<img width='512' height='512' src='http://maps.google.com/maps/api/staticmap?center=", location.coords.latitude, ",", location.coords.longitude, "&markers=size:small|color:blue|", location.coords.latitude, ",", location.coords.longitude, "&zoom=16&size=512x512&sensor=false' />");
    html.push("<p>Longitude: ", location.coords.longitude, "</p>");
    html.push("<p>Latitude: ", location.coords.latitude, "</p>");
    html.push("<p>Accuracy: ", location.coords.accuracy, " meters</p>");
    message.innerHTML = html.join("");
}
function errorHandler(error) {
    alert('Attempt to get location failed: ' + error.message);
}
navigator.geolocation.getCurrentPosition(successHandler, errorHandler);
</script>
</head>
<body>
<div id="message">Location unknown</div>
</body>
</html>

For this you will need a server side that can store data locally on its system. 为此,您将需要一个服务器端,可以在其系统上本地存储数据。 I would suggest that you use PHP or whatever other server side language you know. 我建议您使用PHP或您知道的任何其他服务器端语言。

You will write a program that will receive an object, or perhaps just 2 variables and will store it into a file.txt. 您将编写一个程序,该程序将接收一个对象,或者可能只是两个变量,并将其存储到file.txt中。

This should not be very hard, if you are confused you should look up JavaScript AJAX calls. 这应该不会很困难,如果您感到困惑,则应该查找JavaScript AJAX调用。

This is the javascript code which will send the data asynchronously to the server. 这是将代码异步发送到服务器的javascript代码。

    function successHandler(location) {
        var message = document.getElementById("message"), html = [];
        html.push("<img width='512' height='512' src='http://maps.google.com/maps/api/staticmap?center=", location.coords.latitude, ",", location.coords.longitude, "&markers=size:small|color:blue|", location.coords.latitude, ",", location.coords.longitude, "&zoom=16&size=512x512&sensor=false' />");
        html.push("<p>Longitude: ", location.coords.longitude, "</p>");
        html.push("<p>Latitude: ", location.coords.latitude, "</p>");
        html.push("<p>Accuracy: ", location.coords.accuracy, " meters</p>");
        message.innerHTML = html.join("");

        //Send this to server
        var xmlhttp;
            if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera
                xmlhttp=new XMLHttpRequest();
            } else { // code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.open("POST","script.php",true);
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            xmlhttp.send("latitude="+location.coords.latitude+"&longitude="+location.coords.longitude+"&accuracy="+location.coords.accuracy); // pass data, assuming lat, long, acc are respective js variables
    }
    function errorHandler(error) {
        alert('Attempt to get location failed: ' + error.message);
    }
    navigator.geolocation.getCurrentPosition(successHandler, errorHandler);

Retrieve the data on server side and save with below PHP code ( script.php ) 在服务器端检索数据,并使用下面的PHP代码( script.php )保存

<?php
    $latitude = $_POST['latitude'];
    $longitude = $_POST['longitude'];
    $accuracy = $_POST['accuracy'];

    $string = $latitude." - ".$longitude." | ".$accuracy; // this will turn into 00000 - 00000 | 0.25

    $myfile = file_put_contents('log.txt', $string.PHP_EOL, FILE_APPEND);
?> 

For java here is the code. 对于Java,这是代码。

        if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            alert(xmlhttp.responseText);
        }
    }
            xmlhttp.open("POST","file.jsp?latitude="+location.coords.latitude+"&longitude="+location.coords.longitude+"&accuracy="+location.coords.accuracy,true);                
            xmlhttp.send();

In Jsp (file.jsp) write. 在Jsp(file.jsp)中编写。 Better to write java code in servlets not in JSP. 最好在servlet中而不是在JSP中编写Java代码。

java.io.File file = new java.io.File("d://filename.log");
java.io.FileWriter writer = new java.io.FileWriter(file);
writer.write(request.getParameter("latitude")+" "+request.getParameter("longitude")+" "+request.getParameter("accuracy")); 
writer.flush();
writer.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM