首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让webshim polyfill库在React中工作?

如何让webshim polyfill库在React中工作?
EN

Stack Overflow用户
提问于 2019-06-25 17:57:52
回答 1查看 97关注 0票数 0

我想从webshim库访问地理定位功能,但我找不到正确的设置来使其工作?

我已经访问了内置的浏览器geolocation功能,但希望在没有geolocation功能的浏览器中设置polyfill。

webshim

代码语言:javascript
复制
    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")
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2019-06-25 22:13:09

使用polyfill来填充对地理位置的支持将不起作用。从浏览器获取位置需要本机支持。

几乎所有的浏览器都支持地理定位,https://caniuse.com/#feat=geolocation

相反,您应该检查浏览器是否支持地理位置。如果它不被支持,失败并宽限(向用户显示一些错误)

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56751382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档