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

时间:2015-07-14 13:32:35

标签: javascript php html geolocation

我有这个代码它显示地理位置(经度:xx纬度:xx准确度:xxx)..当任何正文访问网址时,我可以将结果输出到日志文件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>

3 个答案:

答案 0 :(得分:0)

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

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

这应该不是很难,如果你感到困惑,你应该查看JavaScript AJAX调用。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

在Jsp(file.jsp)中写。最好在不在JSP中的servlet中编写java代码。

{{1}}