[英]How can I make Geolocation API function working on localhost file://?
I have this basic code to retrieve latitude and longitude with Geolocation API :我有这个基本代码来使用 Geolocation API 检索纬度和经度:
showmap.html显示地图.html
<button onclick="getLocation()">Find me </button>
<script type="text/javascript" src="js/geolocation.js"></script>
js/geolocation.js js/geolocation.js
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("Geolocation is not supported by your browser. Please update your browser. Visit Help Center.");
}
}
function showPosition(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
console.log("Latitude : " + latitude + "<br> Longitude :" + longitude);
}
When I click on the button I receive this error in the console当我单击按钮时,我在控制台中收到此错误
SyntaxError: An invalid or illegal string was specified showmap.html:72
_sendMessage file:///home/user/Desktop/Projects/showmap.html:72
call file:///home/user/Desktop/Projects/showmap.html:64
getCurrentPosition file:///home/user/Desktop/Projects/showmap.html:16
getLocation file:///home/user/Desktop/Projects/js/geolocation.js:4
onclick file:///home/user/Desktop/Projects/showmap.html:1
fireMouseEvent resource://devtools/server/actors/emulation/touch-simulator.js:290
dispatchMouseEvents resource://devtools/server/actors/emulation/touch-simulator.js:264
I'm trying to figure out how to solve this issue.我想弄清楚如何解决这个问题。 Is this not working because I'm in file://
?这是不是因为我在file://
而不起作用?
To get file:
to work with Geolocation Api, run ngrok
and use the https
url to host your HTML app.要获取file:
要使用 Geolocation Api,请运行ngrok
并使用https
url 来托管您的 HTML 应用程序。 No need for PHP or backend language like Python, you can generate a local tunnel using your credentials.不需要 PHP 或 Python 等后端语言,您可以使用您的凭据生成本地隧道。
This assumes you have ngrok installed and that you are running Ubuntu.这假设您已经安装了 ngrok 并且您正在运行 Ubuntu。
Open terminal and run :打开终端并运行:
ngrok http -auth="user:password" file:///path/to/your/HTML/project
Reference : Serving local directories with ngrok's built-in fileserver https://ngrok.com/docs参考:使用 ngrok 的内置文件服务器为本地目录提供服务https://ngrok.com/docs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.