1、利用h5 屬性獲取用戶地理位置
h5的新增屬性是支持用戶獲取地理位置的特別是手機,支持的情況會更好。具體寫法如下
// 定位功能 getLocation () {if (navigator.geolocation) {navigator.geolocation.getCurrentPosition(showPosition, showError);} else {alert('瀏覽器不支持地理定位。');}},
// 定位成功showPosition (position) {var lat = position.coords.latitude;// 緯度var lag = position.coords.longitude;// 經度alert('緯度:' + lat + ',經度:' + lag);},
// 處理失敗showError (error) {switch (error.code) {case error.PERMISSION_DENIED:alert('定位失敗,用戶拒絕請求地理定位');break;case error.POSITION_UNAVAILABLE:alert('定位失敗,位置信息是不可用');break;case error.TIMEOUT:alert('定位失敗,請求獲取用戶位置超時');break;case error.UNKNOWN_ERROR:alert('定位失敗,定位系統失效');break;}}
只需要這三個函數便可以了。執行
getLocation ()
這個函數便可以獲取經緯度了
?