Chrome地理位置信息模拟

Chrome经纬度座标信息模拟

通过Chrome的Sensor面板可以设置经纬度与时区。首先我们要设置允许获取经纬度数据。

在地址栏输入chrome://settings/content/location[1]进入位置信息面板。首先设置Chrome允许获取位置信息,如果某些网站位置信息被禁止获取,要从对应的列表中排除,否则提示没有权限。

接着在Sensors面板中可以设置默认区域,或者我们可以自定义一个区域并指定经纬度。
在这里插入图片描述
接着我们遍可以通过navigator.geolocation[2]来获取经纬度数据进行验证。

var options = {
  enableHighAccuracy: true,
  timeout: 5000,
  maximumAge: 0
};

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);
};

navigator.geolocation.getCurrentPosition(success, error, options);

输出如下:

Your current position is:
VM161:11 Latitude : 52.520007
VM161:12 Longitude: 13.404954
VM161:13 More or less 150 meters.

参考

[1]地理位置,https://developers.google.com/web/tools/chrome-devtools/device-mode/geolocation
[2]getCurrentPosition,https://developer.mozilla.org/zh-CN/docs/Web/API/Geolocation/getCurrentPosition

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章