Javascript 如何在iframe中使用来自传单.js的地图
我正试图利用iframe,通过使用地理定位API,将地图的标记定位在用户设备所在的位置。这是因为此错误消息Javascript 如何在iframe中使用来自传单.js的地图,javascript,html,iframe,leaflet,geolocation,Javascript,Html,Iframe,Leaflet,Geolocation,我正试图利用iframe,通过使用地理定位API,将地图的标记定位在用户设备所在的位置。这是因为此错误消息错误(1):权限策略已在此文档中禁用地理位置。。我已经尝试在入门页面中包含来自传单.js文档的地图div;包含在我的iframe元素中,属性为allow=“geolocation”,但这似乎不起作用,有人能帮忙吗 这是假定获取用户位置的代码 function success(position) { var crd = position.coords; console.log('Yo
错误(1):权限策略已在此文档中禁用地理位置。
。我已经尝试在入门页面中包含来自传单.js文档的地图div;包含在我的iframe元素中,属性为allow=“geolocation”
,但这似乎不起作用,有人能帮忙吗
这是假定获取用户位置的代码
function success(position) {
var crd = position.coords;
console.log('Your current position is:');
console.log(`Latitude : ${crd.latitude}`);
console.log(`Longitude: ${crd.longitude}`);
console.log(`More or less ${crd.accuracy} meters.`);
}
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
}
navigator.geolocation.getCurrentPosition(success, error, options);
使用leaflejs中内置的locate函数不是更好吗? 您所犯的错误也可能是因为 这里有一个完整的使用iframe的过程
map
.locate({
// https://leafletjs.com/reference-1.7.1.html#locate-options-option
setView: true,
enableHighAccuracy: true,
})
// if location found show marker and circle
.on('locationfound', (e) => {
console.log(e);
// marker
const marker = L.marker([e.latitude, e.longitude]).bindPopup(
'Your are here :)'
);
// circle
const circle = L.circle([e.latitude, e.longitude], e.accuracy / 2, {
weight: 2,
color: 'red',
fillColor: 'red',
fillOpacity: 0.1,
});
// add marker
map.addLayer(marker);
// add circle
map.addLayer(circle);
})
// if error show alert
.on('locationerror', (e) => {
console.log(e);
alert('Location access denied.');
});