0

我想从 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")
        }
    }

4

1 回答 1

0

使用 polyfill 来填充对 Geolocations 的支持是行不通的。从浏览器获取位置需要本机支持。

几乎所有的浏览器都支持地理定位,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
}
于 2019-06-25T14:13:08.840 回答