数据载入中,请稍后......
 
展开
懒人码库客户端 点击或扫描下载
欢迎来到懒人码库!懒人码库QQ群:19651313
欢迎来到懒人码库!懒人码库QQ群:19651313  
当前位置:懒人码库 > HTML教程 > HTML5

利用HTML5定位功能,实现在百度地图上定位 :

测试浏览器:ie11定位成功率100%,Safari定位成功率97%。其它浏览器暂时不行,HTML5获取不了地理位置信息。

  1. <!DOCTYPE html> 
  2. <html lang="en"> 
  3. <head> 
  4.     <meta charset="UTF-8"> 
  5.     <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'> 
  6.     <title>HTML5定位</title> 
  7.     <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"></script> 
  8.   <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=wqBXfIN3HkpM1AHKWujjCdsi"></script> 
  9.   <script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script> 
  10.     <style type="text/css"> 
  11.     *{ margin: 0px; padding: 0px;} 
  12.   body{text-align: center;  height: 100%;overflow:hidden;} 
  13.   #allmap{ width: 100%;height: 100%; position: absolute;} 
  14.     </style> 
  15. </head> 
  16. <body> 
  17.     <div id="allmap"></div> 
  18. <script type="text/javascript"> 
  19.  $(function(){ 
  20.      if(supportsGeoLocation()){ 
  21.         alert("你的浏览器支持 GeoLocation."); 
  22.      }else{ 
  23.         alert("不支持 GeoLocation.") 
  24.      } 
  25.   // 检测浏览器是否支持HTML5 
  26.                function supportsGeoLocation(){ 
  27.                 return !!navigator.geolocation; 
  28.               }   
  29.   // 单次位置请求执行的函数              
  30.                function getLocation(){ 
  31.                   navigator.geolocation.getCurrentPosition(mapIt,locationError); 
  32.                } 
  33.   //定位成功时,执行的函数 
  34.               function mapIt(position){  
  35.                 var lon = position.coords.longitude; 
  36.                 var lat = position.coords.latitude; 
  37.                 // alert("您位置的经度是:"+lon+" 纬度是:"+lat); 
  38.                 var map = new BMap.Map("allmap"); 
  39.                 var point = new BMap.Point(""+lon+"",""+lat+""); 
  40.                 map.centerAndZoom(point,19); 
  41.                 var gc = new BMap.Geocoder(); 
  42.                       translateCallback = function (point){ 
  43.                           var marker = new BMap.Marker(point); 
  44.                           map.addOverlay(marker); 
  45.                           map.setCenter(point); 
  46.                           gc.getLocation(point, function(rs){ 
  47.                                 var addComp = rs.addressComponents; 
  48.                                 if(addComp.province!==addComp.city){ 
  49.                                   var sContent = 
  50.                                   "<div><h4 style='margin:0 0 5px 0;padding:0.2em 0'>你当前的位置是:</h4>" +  
  51.                                   "<p style='margin:0;line-height:1.5;font-size:13px;text-indent:2em'>"+addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber+"</p>" +  
  52.                                   "</div>";} 
  53.                                 else{ 
  54.                                   var sContent = 
  55.                                   "<div><h4 style='margin:0 0 5px 0;padding:0.2em 0'>你当前的位置是:</h4>" +  
  56.                                   "<p style='margin:0;line-height:1.5;font-size:13px;text-indent:2em'>"+ addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber+"</p>" +  
  57.                                   "</div>"; 
  58.                                 } 
  59.                                 var infoWindow = new BMap.InfoWindow(sContent); 
  60.                                 map.openInfoWindow(infoWindow,point); 
  61.                           });  
  62.                       }                     
  63.                   BMap.Convertor.translate(point,0,translateCallback);  
  64.             } 
  65.   // 定位失败时,执行的函数 
  66.                function locationError(error) 
  67.               { 
  68.               switch(error.code) 
  69.                 { 
  70.                 case error.PERMISSION_DENIED: 
  71.                   alert("User denied the request for Geolocation."); 
  72.                   break; 
  73.                 case error.POSITION_UNAVAILABLE: 
  74.                    alert("Location information is unavailable."); 
  75.                   break; 
  76.                 case error.TIMEOUT: 
  77.                    alert("The request to get user location timed out."); 
  78.                   break; 
  79.                 case error.UNKNOWN_ERROR: 
  80.                    alert("An unknown error occurred."); 
  81.                   break; 
  82.                 } 
  83.               } 
  84.   // 页面加载时执行getLocation函数 
  85.   window.onload = getLocation;   
  86.         }) 
  87. </script> 
  88. </body> 
  89. </html> 

小贴士:懒人码库 为网页前端人员提供建站常用的广告代码和网页特效,内容涵盖焦点图,导航菜单,jQuery代码,图片特效,对联广告,日历控件,在线客服等。