透过 Geolocation API 可以让 Progressive Web App 存取用户的位置,方便实作地理资讯相关应用。
https://get.geojs.io/v1/ip/geo.json
if (navigator.geolocation) {
console.log("Geolocation is supported!");
} else {
console.log("Geolocation is not supported for this Browser/OS.");
}
navigator.geolocation.getCurrentPosition
一次性取得用户地理位置资讯。var startPos;
var geoSuccess = function (position) {
startPos = position;
console.log(
"lat",
startPos.coords.latitude,
"lng",
startPos.coords.longitude
);
};
navigator.geolocation.getCurrentPosition(geoSuccess);
navigator.geolocation.watchPosition
持续追踪用户地理位置资讯
var id, target, options;
function success(pos) {
var crd = pos.coords;
if (target.latitude === crd.latitude && target.longitude === crd.longitude) {
console.log("成功追踪");
navigator.geolocation.clearWatch(id);
}
}
function error(err) {
console.warn("ERROR(" + err.code + "): " + err.message);
}
target = {
latitude: 0,
longitude: 0,
};
options = {
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 0,
};
id = navigator.geolocation.watchPosition(success, error, options);
clearWatch
navigator.geolocation.clearWatch(id);
让使用者同意位置共用的时机,在 App 启动时就请求权限会是最烂的时机,而且对於在页面载入时要求提供位置的网站,用户通常会不信任。
在 Android 5.0 之後的实作上也是在要使用时才会向使用者请求权限,所以在实作上要假设:
在用户使用特定功能时才让用户主动操作并出现提示,且要准备好使用被拒绝的备案。
前言 闭包,一个完全无法从字面意思了解的专有名词,若是改叫小笼闭包,是不是马上联想到这个画面 一个个...
不知不觉来到铁人赛的尾声了,此次系列文的初衷除了自我复习与归纳之前参加比赛的经验,也希望尽量统整一...
我们终於开始写部落格前台的部分, 我们先将首页改成所有人的图像列表, 然後点进去可以看到这个使用者的...
物件结构 // 物件实字 var farm = { dog : '小黑', duck : 20, f...
YARN YARN(Yet Another Resource Negotiator) 是 Hado...