我在使用传单中的定位功能时遇到问题。
我正在使用 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/