代碼如下:<br><br>/**?<br>*?以下為html5代碼,獲取地理位置?<br>*/?<br>function?getLocation()?{?<br>//檢查瀏覽器是否支持地理位置獲取?<br>if?(navigator.geolocation)?{?<br>//若支持地理位置獲取,成功調用showPosition(),失敗調用showError?<br>//?alert("正在努力獲取位置...");?<br>var?config?=?{?enableHighAccuracy:?true,?timeout:?5000,?maximumAge:?30000?};?<br>navigator.geolocation.getCurrentPosition(showPosition,?showError,?config);?<br>}?else?{?<br>//alert("Geolocation?is?not?supported?by?this?browser.");?<br>alert("標注失敗,用戶已禁用位置獲取權限");?<br>}?<br>}?<br>/**?<br>*?獲取地址位置成功?<br>*/?<br>function?showPosition(position)?{?<br>//獲得經度緯度?<br>var?x?=?position.coords.latitude;?<br>var?y?=?position.coords.longitude;?<br>//配置Baidu?Geocoding?API?<br>var?url?=?""?+?<br>"&callback=renderReverse"?+?<br>"&location="?+?x?+?","?+?y?+?<br>"&output=json"?+?<br>"&pois=0";?<br>$.ajax({?<br>type:?"GET",?<br>dataType:?"jsonp",?<br>url:?url,?<br>success:?function?(json)?{?<br>if?(json?==?null?||?typeof?(json)?==?"undefined")?{?<br>return;?<br>}?<br>if?(json.status?!=?"0")?{?<br>return;?<br>}?<br>setAddress(json.result.addressComponent);?<br>},?<br>error:?function?(XMLHttpRequest,?textStatus,?errorThrown)?{?<br>alert("[x:"?+?x?+?",y:"?+?y?+?"]地址位置獲取失敗,請手動選擇地址");?<br>}?<br>});?<br>}?<br>/**?<br>*?獲取地址位置失敗[暫不處理]?<br>*/?<br>function?showError(error)?{?<br>switch?(error.code)?{?<br>case?error.PERMISSION_DENIED:?<br>alert("標注失敗,用戶拒絕請求地理標注");?<br>//x.innerHTML?=?"User?denied?the?request?for?Geolocation.[用戶拒絕請求地理標注]"?<br>break;?<br>case?error.POSITION_UNAVAILABLE:?<br>alert("標注失敗,位置信息是不可用");?<br>//x.innerHTML?=?"Location?information?is?unavailable.[位置信息是不可用]"?<br>break;?<br>case?error.TIMEOUT:?<br>alert("標注失敗,請求獲取用戶位置超時");?<br>//x.innerHTML?=?"The?request?to?get?user?location?timed?out.[請求獲取用戶位置超時]"?<br>break;?<br>case?error.UNKNOWN_ERROR:?<br>alert("標注失敗,標注系統失效");?<br>//x.innerHTML?=?"An?unknown?error?occurred.[未知錯誤]"?<br>break;?<br>}?<br>}?<br>/**?<br>*?設置地址?<br>*/?<br>function?setAddress(json)?{?<br>var?position?=?document.getElementById("txtPosition");?<br>//省?<br>var?province?=?json.province;?<br>//市?<br>var?city?=?json.city;?<br>//區?<br>var?district?=?json.district;?<br>province?=?province.replace('市',?'');?<br>position.value?=?province?+?","?+?city?+?","?+?district;?<br>position.style.color?=?'black';?<br>}