我想从webshim库访问地理定位功能,但我找不到正确的设置来使其工作?
我已经访问了内置的浏览器geolocation功能,但希望在没有geolocation功能的浏览器中设置polyfill。
import React from "react";
import webshim from 'webshim';
import $ from 'jquery';
class PlayGround extends React.Component{
pickLocation = () => {
console.log("Inside here")
webshim.ready('geolocation', () => {
navigator.geolocation.getCurrentPosition(function(pos){
alert("Thx, you are @ latitude: "+ pos.coords.latitude +"/longitude: " + pos.coords.longitude);
});
});
console.log("end inside")
}
}发布于 2019-06-25 22:13:09
使用polyfill来填充对地理位置的支持将不起作用。从浏览器获取位置需要本机支持。
几乎所有的浏览器都支持地理定位,https://caniuse.com/#feat=geolocation
相反,您应该检查浏览器是否支持地理位置。如果它不被支持,失败并宽限(向用户显示一些错误)
function fetchLocation() {
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
navigator.geolocation.getCurrentPosition(success, error, options);
}
function success(pos) {
var crd = pos.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}`);
}
if(window.location.protocol == "https:" && navigator.geolocation) {
fetchLocation();
} else {
// We cannot access the geolocation, show some error
}https://stackoverflow.com/questions/56751382
复制相似问题