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

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