HTML5 ????
HTML5 ????
HTML5 ????? ???? ??? ???? ? ?????.
??? ?? ??
HTML5 Geolocation API? ???? ??? ??? ?? ? ?????.
? ??? ???? ????? ??? ? ????, ???? ?? ??? ???? ????? ??? ? ????.
???? ??
Internet Explorer 9+, Firefox, Chrome, Safari ? Opera?? ????? ?????.
??: ????? iPhone? ?? GPS? ?? ???? ? ?????.
????? ??? ????? ?????.
if (navigator.geolocation) { //console.log("瀏覽器支持!"); }else { // console.log("瀏覽器不支持!"); }
navigator.geolocation? ????? ???? ?? ???? ??? ??? ?? ? ?????. ???:
void getCurrentPosition(onSuccess,onError,options);//獲取用戶當(dāng)前位置 int watchCurrentPosition(onSuccess,onError,options);//持續(xù)獲取當(dāng)前用戶位置 void clearWatch(watchId);//watchId 為watchCurrentPosition返回的值//取消監(jiān)控rrree
?? ? ?? ??
getCurrentPosition() ???? ? ?? ????? ??? ???? ? ?????. ??? ?? ?? ?? ? ??? ??? ?????:
Instance
實(shí)例: <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>基于瀏覽器的HTML5查找地理位置</title> <!-- 百度API --> <script src="http://api.map.baidu.com/api?v=1.2" type="text/javascript"></script> <script> function getLocation(){ var options={ enableHighAccuracy:true, maximumAge:1000 } if(navigator.geolocation){ //瀏覽器支持geolocation navigator.geolocation.getCurrentPosition(onSuccess,onError,options); }else{ //瀏覽器不支持geolocation } } //成功時(shí) function onSuccess(position){ //返回用戶位置 //經(jīng)度 var longitude =position.coords.longitude; //緯度 var latitude = position.coords.latitude; //使用百度地圖API //創(chuàng)建地圖實(shí)例 var map =new BMap.Map("container"); //創(chuàng)建一個(gè)坐標(biāo) var point =new BMap.Point(longitude,latitude); //地圖初始化,設(shè)置中心點(diǎn)坐標(biāo)和地圖級(jí)別 map.centerAndZoom(point,15); } //失敗時(shí) function onError(error){ switch(error.code){ case 1: alert("位置服務(wù)被拒絕"); break; case 2: alert("暫時(shí)獲取不到位置信息"); break; case 3: alert("獲取信息超時(shí)"); break; case 4: alert("未知錯(cuò)誤"); break; } } window.onload=getLocation; </script> </head> <body <div id="container" style="width:600px;height:600px"></div> </body> </html>