javascript - 使用 Leaflet 和本地主机拒绝位置访问

标签 javascript express leaflet

我在使用传单中的定位功能时遇到问题。

我正在使用 express 通过本地主机提供我的应用程序。当我从我自己的计算机访问它时它可以工作,但是当我使用网络上的另一台计算机时它拒绝位置访问。

在网络和权限方面,我不是很了解。任何帮助,将不胜感激!

用于 express 的 App.js

var express     = require("express"),
    app         = express(),
    router      = express.Router(),
    port        = process.env.PORT || 3000,
    bodyParser  = require("body-parser");

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + "/public"));
app.use(express.static(__dirname + "/views"));
app.use(express.static(__dirname + "/plugins"));

app.get("/", function(req, res){
    res.sendFile("index.html");
});

app.listen(port, function(){
    console.log("app is running on port " + port);
});

索引.js:

    mymap.locate({setView: true})
      .on("locationfound", function(e){
        if(currentLoc){
          currentLoc.remove();
          currentLocErr.remove();
        }
        currentLoc = L.marker([e.latitude, e.longitude]).bindPopup('Your location');
        currentLocErr = L.circle([e.latitude, e.longitude], e.accuracy/2, {
            weight: 1,
            color: 'blue',
            fillColor: '#cacaca',
            fillOpacity: 0.5
          });
        mymap.addLayer(currentLoc);
        mymap.addLayer(currentLocErr); 
      })
      .on("locationerror", function(e){
        alert("Location access denied.");
    });

最佳答案

来自 docs :

locate(< Locate options > options?): Tries to locate the user using the Geolocation API ... Note that, if your page doesn't use HTTPS, this method will fail in modern browsers

那么在您的情况下,您的 localhost 是一个可靠的来源,您的浏览器将允许您使用浏览器本身提供的 Geolocation API,没有问题。

但是,如果您尝试从网络中的另一台计算机访问它,您应该将代码发布为 HTTPS 以使其正常工作,否则它会抛出您遇到的错误。

您可以阅读有关地理定位 API 的更多信息 here .

关于javascript - 使用 Leaflet 和本地主机拒绝位置访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48102394/

相关文章:

javascript - 如何将单选下拉菜单与单选按钮绑定(bind)?

javascript - 在 google puppeteer 上作为 evaluate 方法的参数

node.js - 如何在 Mongoose 中为部分字符串匹配创建搜索查询?

r - 如何在传单中的两个标记之间添加路线(折线)

javascript - Js RegEx 不识别重音字母 (utf-8)

javascript - 在 IE 上开发的 Javascript 在 Firefox 上失败的典型原因是什么?

javascript - 字符串传递给另一个函数后变成对象?

javascript - LeafLet 错误 : Object #<Object> has no method 'onAdd' at file:///android_asset/www/leaflet. js:6

reactjs - 更新用户信息 sequelize express

node.js - 如何在nodejs和express中的一个地方处理异常