英文:
HTML/Javascript: access to geolocation from local HTML file
问题
我正在尝试实现一个在地图上显示您位置的网页。考虑到访问网页的人可能没有互联网访问权限,我希望网页是一个本地的HTML文件。
出于安全原因,这些天似乎不可能从本地HTML文件访问地理位置信息。
有没有关于如何从本地HTML文件中访问地理位置信息的想法?
谢谢。
我尝试通过本地Web服务器访问本地HTML文件,但这些只允许http页面,而地理位置似乎依赖于https文件。
英文:
I'm trying to implement a web page which displays your location on a map. Given that the person accessing the web page may not have internet access, I want the web page to be a local HTML file.
These days, for security reasons, it doesn't seem to be possible to access geolocation form a local HTML file.
Any ideas as to how I can get access to geolocation from a local HTML file?
Thanks
I have tried accessing my local HTML file via a local web server but these only allow http pages whereas geolocation seems to be dependent upon an https file.
答案1
得分: 1
为了使用地理位置API,您的应用程序应在HTTPS协议或本地主机Web服务器上运行。否则,地理位置API将无法工作。
最重要的是,您将获得您的互联网提供商站点的坐标
navigator.geolocation.getCurrentPosition(showPosition);
function showPosition(position) {
console.log("纬度:" + position.coords.latitude +
" 经度:" + position.coords.longitude);
}
英文:
To use Geolocation API your application should run on HTTPS protocol or on the localhost web server. Otherwise, Geolocation API will not work.
Most of all you will get the coordinates of your internet provider stations
navigator.geolocation.getCurrentPosition(showPosition);
function showPosition(position) {
console.log("Latitude: " + position.coords.latitude +
" Longitude: " + position.coords.longitude);
}
答案2
得分: -2
以下是您要翻译的内容:
<button onclick="getLocation()">获取位置</button>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("此浏览器不支持地理位置信息。");
}
}
function showPosition(position) {
alert("纬度: " + position.coords.latitude +
"\n经度: " + position.coords.longitude);
}
</script>
英文:
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-html -->
<button onclick="getLocation()">Get Location</button>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
alert("Latitude: " + position.coords.latitude +
"\nLongitude: " + position.coords.longitude);
}
</script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论