这是我的地理位置代码
<!-- <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='400' height='400' 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=14&size=256x256&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("");
var javascriptLongitude = location.coords.longitude;
var javascriptLatitude = location.coords.latitude;
var javascriptAccuracy = location.coords.accuracy;
window.location.href = "myphpfile.php?longitude=" + javascriptLongitude + "&" + "latitude=" + javascriptLatitude + "&" + "accuracy=" + javascriptAccuracy;
}
function errorHandler(error) {
alert('Attempt to get location failed: ' + error.message);
}
navigator.geolocation.getCurrentPosition(successHandler, errorHandler);
</script>
</head>
<body>
<a href="http://jquery.com/">jQuery</a>
<script src="jquery.js"></script>
<div id="message">Location unknown</div>
</body>
</html>
我在这4行中设置了变量
var javascriptLongitude = location.coords.longitude;
var javascriptLatitude = location.coords.latitude;
var javascriptAccuracy = location.coords.accuracy;
window.location.href = "myphpfile.php?longitude=" + javascriptLongitude + "&" + "latitude=" + javascriptLatitude + "&" + "accuracy=" + javascriptAccuracy;
这是myphpfile.php代码
<?php
$Longitude = $_GET["longitude"];
$Latitude = $_GET["latitude"];
$Accuracy = $_GET["accuracy"];
$Return = " ".PHP_EOL;
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
fwrite($myfile, $Longitude);
fwrite($myfile, $Return);
fwrite($myfile, $Latitude);
fwrite($myfile, $Return);
fwrite($myfile, $Accuracy);
fclose($myfile);
?>
我现在的主要问题是当我打开该网站时,它会询问我是否允许它知道我的位置,然后我单击“允许”,它会工作一次......然后当我关闭选项卡并再次打开它时,它不会不起作用,并向我显示位置未知,如代码中所示,要解决此问题,我必须单击
我必须点击清除这些设置以供将来访问或转到 chrome://settings/contentExceptions#location 并从那里删除我网站的项目,然后它才能再次工作...并且我想这无法通过手机完成......
我的第二个问题,其实也不是什么大问题,在写入txt文件时,"\n"、"\r\n"和>PHP_EOL 不起作用,尝试添加它们,例如 $Longitude = $_GET["longitude"].PHP_EOL;
和 $Longitude = $_GET["longitude"]。 "\n";
但仍然没有运气
最佳答案
这似乎是 jquery.js 标签阻止了 dom 元素的加载。另外,请务必检查浏览器是否确实内置了此功能,因为许多浏览器仍然没有内置此功能。
`
<div id="message">Loading...</div>
`
function successHandler(location) {
var message = document.getElementById("message"),
html = [];
html.push("<img width='400' height='400' 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=14&size=256x256&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("");
//Uncomment it for it to redirect.
//window.location.href = "myphpfile.php?longitude=" + location.coords.longitude + "&" + "latitude=" + location.coords.latitude + "&" + "accuracy=" + location.coords.accuracy;
}
function errorHandler(error) {
alert('Attempt to get location failed: ' + error.message);
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successHandler, errorHandler);
} else {
alert("Please download a modern browser.");
}
`
关于javascript - 地理位置允许位置异常导致站点无法进入成功功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35046667/