惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
U
Unit 42
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research
罗磊的独立博客
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
L
LangChain Blog
Engineering at Meta
Engineering at Meta
量子位
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
A
About on SuperTechFans
Y
Y Combinator Blog

博客园 - 伽马科技.攻城师

管理git生成的多个ssh key Docker基础教程(命令详解) Docker基础教程(常用命令篇) Docker基础教程(安装篇) 常用的Jquery插件 自定义webkit浏览器滚动条样式 centos tar压缩与解压缩命令大全 Nginx编译安装(Centos) Nginx的启动脚本(Centos) ffmpeg 音频转换(amr2mp3) 安装php openssl扩展 centos手动编译安装apache、php、mysql 添加自编译的apache为linux系统服务 Jquery datatables 使用方法 HTML5 图片缩放功能 七牛图片上传JSSDK 2015年12月中国航空公司名录 HTML5 开发框架 openerp7 时区问题
利用HTML5定位功能,实现在百度地图上定位
伽马科技.攻城师 · 2016-01-20 · via 博客园 - 伽马科技.攻城师
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>HTML5定位</title>
 6     <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"></script>
 7   <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=wqBXfIN3HkpM1AHKWujjCdsi"></script>
 8   <script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
 9     <style type="text/css">
10     *{ margin: 0px; padding: 0px;}
11   body{text-align: center;  height: 100%;overflow:hidden;}
12   #allmap{ width: 100%;height: 100%; position: absolute;}
13     </style>
14 </head>
15 <body>
16     <div id="allmap"></div>
17 <script type="text/javascript">
18  $(function(){
19      if(supportsGeoLocation()){
20          alert("你的浏览器支持 GeoLocation.");
21      }else{
22          alert("不支持 GeoLocation.")
23      }
24   // 检测浏览器是否支持HTML5
25                function supportsGeoLocation(){
26                   return !!navigator.geolocation;
27               }  
28   // 单次位置请求执行的函数             
29                function getLocation(){
30                   navigator.geolocation.getCurrentPosition(mapIt,locationError);
31                }
32   //定位成功时,执行的函数
33               function mapIt(position){ 
34                 var lon = position.coords.longitude;
35                    var lat = position.coords.latitude;
36                    // alert("您位置的经度是:"+lon+" 纬度是:"+lat);
37                 var map = new BMap.Map("allmap");
38                 var point = new BMap.Point(""+lon+"",""+lat+"");
39                 map.centerAndZoom(point,19);
40                 var gc = new BMap.Geocoder();
41                       translateCallback = function (point){
42                           var marker = new BMap.Marker(point);
43                           map.addOverlay(marker);
44                           map.setCenter(point);
45                           gc.getLocation(point, function(rs){
46                                 var addComp = rs.addressComponents;
47                                 if(addComp.province!==addComp.city){
48                                   var sContent =
49                                   "<div><h4 style='margin:0 0 5px 0;padding:0.2em 0'>你当前的位置是:</h4>" + 
50                                   "<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>" + 
51                                   "</div>";}
52                                 else{
53                                   var sContent =
54                                   "<div><h4 style='margin:0 0 5px 0;padding:0.2em 0'>你当前的位置是:</h4>" + 
55                                   "<p style='margin:0;line-height:1.5;font-size:13px;text-indent:2em'>"+ addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber+"</p>" + 
56                                   "</div>";
57                                 }
58                                 var infoWindow = new BMap.InfoWindow(sContent);
59                                 map.openInfoWindow(infoWindow,point);
60                           }); 
61                       }                    
62                   BMap.Convertor.translate(point,0,translateCallback); 
63             }
64   // 定位失败时,执行的函数
65                function locationError(error)
66               {
67               switch(error.code)
68                 {
69                 case error.PERMISSION_DENIED:
70                   alert("User denied the request for Geolocation.");
71                   break;
72                 case error.POSITION_UNAVAILABLE:
73                    alert("Location information is unavailable.");
74                   break;
75                 case error.TIMEOUT:
76                    alert("The request to get user location timed out.");
77                   break;
78                 case error.UNKNOWN_ERROR:
79                    alert("An unknown error occurred.");
80                   break;
81                 }
82               }
83   // 页面加载时执行getLocation函数
84   window.onload = getLocation;  
85         })
86 </script>
87 </body>
88 </html>