繁体   English   中英

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

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

我有这段代码,它显示地理位置(经度: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>

为此,您将需要一个服务器端,可以在其系统上本地存储数据。 我建议您使用PHP或您知道的任何其他服务器端语言。

您将编写一个程序,该程序将接收一个对象,或者可能只是两个变量,并将其存储到file.txt中。

这应该不会很困难,如果您感到困惑,则应该查找JavaScript AJAX调用。

这是将代码异步发送到服务器的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);

在服务器端检索数据,并使用下面的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);
?> 

对于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();

在Jsp(file.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