鍍金池/ 問答/HTML5  HTML/ html5獲取用戶地理位置

html5獲取用戶地理位置

html5獲取用戶地理位置 w3c的獲取不了 。
怎么能像餓了么 美團(tuán)那樣獲取用戶所在的城市區(qū)域呢

回答
編輯回答
疚幼

H5 只會(huì)提供坐標(biāo)值給你,如果想要獲取精確的省市區(qū)位置,可以使用百度、高德的 api 去獲取。注意瀏覽器權(quán)限,原生的 geolocation 是無法在 http 下生效的,只能在 https 有效果。

2017年8月11日 06:16
編輯回答
萌小萌

可以通過H5的內(nèi)置程序獲得:

if (navigator.geolocation) {
  var timeoutVal = 10 * 1000 * 1000;
  navigator.geolocation.getCurrentPosition(
    displayPosition, 
    displayError,
    { enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
  );
}
else {
  alert("Geolocation is not supported by this browser");
}

function displayPosition(position) {
  alert("Latitude: " + position.coords.latitude + ", Longitude: " + position.coords.longitude);
}

function displayError(error) {
  var errors = { 
    1: '申請(qǐng)拒絕',
    2: '無法精確匹配地址',
    3: '請(qǐng)求超時(shí)'
  };
  alert("錯(cuò)誤: " + errors[error.code]);
}

瀏覽器支持:

  • Firefox 3.5+
  • Chrome 5.0+
  • Safari 5.0+
  • Opera 10.60+
  • Internet Explorer 9.0+

手機(jī)支持:

  • Android 2.0+
  • iPhone 3.0+
  • Opera Mobile 10.1+
  • Symbian (S60 3rd & 5th generation)
  • Blackberry OS 6
2017年12月4日 04:51