javascript - 地理位置允许位置异常导致站点无法进入成功功能

标签 javascript php geolocation

这是我的地理位置代码

<!-- <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);
?>

我现在的主要问题是当我打开该网站时,它会询问我是否允许它知道我的位置,然后我单击“允许”,它会工作一次......然后当我关闭选项卡并再次打开它时,它不会不起作用,并向我显示位置未知,如代码中所示,要解决此问题,我必须单击 that little location icon

我必须点击清除这些设置以供将来访问或转到 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.");
 }

`

https://jsfiddle.net/iain17/ratzv34t/

关于javascript - 地理位置允许位置异常导致站点无法进入成功功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35046667/

相关文章:

php - 我正在尝试将一个准备好的语句包含在另一个准备好的语句中

javascript - Bootstrap 轮播模板不起作用

javascript - D3图表不显示

php - 检查密码的有效方法在数据库中

PHP 获取 DOMNode 的 XPath

iphone - 如何在iPhone上实现室内导航

javascript - 如何忽略 StandardJS/ESLint 中的规则?

javascript - 我如何禁用 Angular ui Timepicker 中的分钟输入

postgresql - 如何使用 Postgis 查找我所在城市的所有街道交叉口

html - navigator.geolocation.getCurrentPosition 回调在 Firefox 10 上不起作用