Javascript 如何使Geolocation API函数在localhost文件上工作://?

Javascript 如何使Geolocation API函数在localhost文件上工作://?,javascript,geolocation,Javascript,Geolocation,我有以下使用地理定位API检索纬度和经度的基本代码: showmap.html <button onclick="getLocation()">Find me </button> <script type="text/javascript" src="js/geolocation.js"></script> 我正在想办法解决这个问题。这不起作用是因为我在文件中://?要获取文件:要使用地理定位Api,请运行ngrok并使用httpsurl托管

我有以下使用地理定位API检索纬度和经度的基本代码:

showmap.html

<button  onclick="getLocation()">Find me </button>

<script type="text/javascript" src="js/geolocation.js"></script>

我正在想办法解决这个问题。这不起作用是因为我在
文件中://

要获取
文件:
要使用地理定位Api,请运行
ngrok
并使用
https
url托管HTML应用程序。不需要PHP或Python之类的后端语言,您可以使用凭证生成本地隧道

这假设您已经安装了ngrok,并且正在运行Ubuntu

打开终端并运行:

ngrok http -auth="user:password" file:///path/to/your/HTML/project
参考: 使用ngrok的内置文件服务器为本地目录提供服务

这是不正确的协议。这是对您问题的回答:。您提供的代码在test@StackSlave:有趣。什么意思?你能详细说明一下吗?Thanks@marcelo:根据该线程文件中的对话://应该不是问题,因为它是从本地主机运行的,是吗?@saturnconction否,
文件://
不是托管服务器,而是本地计算机的文件系统。与服务器交互时,您使用
http://
协议。参见marcelo提出的dup中guest271314的答案。
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
ngrok http -auth="user:password" file:///path/to/your/HTML/project