码迷,mamicode.com
首页 > Web开发 > 详细

利用html5 共享地理位置

时间:2015-12-20 19:10:28      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

该例子是演示利用HTML5实现地理定位。也就是获得本机IP地址,然后判断出所在位置,并且显示在Google地图上。。

实例效果图

技术分享

 

技术分享

该实例预览地址:http://html5demos.com/geo

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPE html>  
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />  
  5. <meta name="viewport" content="width=620" />  
  6. <title>HTML5 Demo: geolocation</title>  
  7. <style>  
  8. body {  
  9.     font: normal 16px/20px Helvetica, sans-serif;  
  10.     background: rgb(237, 237, 236);  
  11.     margin: 0;  
  12.     margin-top: 40px;  
  13.     padding: 0;  
  14. }  
  15. section, header, footer {  
  16.     display: block;  
  17. }  
  18. #wrapper {  
  19.     width: 600px;  
  20.     margin: 0 auto;  
  21.     background: #fff url(images/shade.jpg) repeat-x center bottom;  
  22.     -moz-border-radius: 10px;  
  23.     -webkit-border-radius: 10px;  
  24.     border-top: 1px solid #fff;  
  25.     padding-bottom: 76px;  
  26. }  
  27. h1 {  
  28.     padding-top: 10px;  
  29. }  
  30. h2 {  
  31.     font-size: 100%;  
  32.     font-style: italic;  
  33. }  
  34. header, article > *, footer > * {  
  35.     margin: 20px;  
  36. }  
  37. footer > * {  
  38.     margin: 20px;  
  39.     color: #999;  
  40. }  
  41. #status {  
  42.     padding: 5px;  
  43.     color: #fff;  
  44.     background: #ccc;  
  45. }  
  46. #status.fail {  
  47.     background: #c00;  
  48. }  
  49. #status.success {  
  50.     background: #0c0;  
  51. }  
  52. </style>  
  53. <script src="h5utils.js"></script><script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>  
  54. </head>  
  55. <body>  
  56. <section id="wrapper">  
  57.   <header>  
  58.     <h1>Geolocation</h1>  
  59.   </header>  
  60.   <article>  
  61.     <p>Finding your location: <span id="status">checking…</span></p>  
  62.   </article>  
  63.   <footer><href=http://www.houoop.com/%3Cspan class="str">"/">HTML5 demo</a></footer>  
  64. </section>  
  65. <script>   
  66. function success(position) {  
  67.     var s = document.querySelector(‘#status‘);  
  68.     if (s.className == ‘success‘) {   
  69.     // not sure why we‘re hitting this twice in FF, I think it‘s to do with a cached result coming back   
  70.     return; }   
  71.     s.innerHTML = "found you!";  
  72.     s.className = ‘success‘;   
  73.     var mapcanvas = document.createElement(‘div‘);   
  74.     mapcanvas.id = ‘mapcanvas‘;   
  75.     mapcanvas.style.height = ‘400px‘;   
  76.     mapcanvas.style.width = ‘100%‘;   
  77.     document.querySelector(‘article‘).appendChild(mapcanvas);   
  78.     var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);   
  79.     var myOptions = {   
  80.          zoom: 15, center: latlng,  
  81.          mapTypeControl: false,   
  82.          navigationControlOptions: {  
  83.              style: google.maps.NavigationControlStyle.SMALL  
  84.              },   
  85.         mapTypeId: google.maps.MapTypeId.ROADMAP };  
  86.         var map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions);  
  87.         var marker = new google.maps.Marker({ position: latlng, map: map, title:"You are here!" });  
  88.         }   
  89.     function error(msg) {  
  90.         var s = document.querySelector(‘#status‘);   
  91.         s.innerHTML = typeof msg == ‘string‘ ? msg : "failed";  
  92.         s.className = ‘fail‘;   
  93.         // console.log(arguments);  
  94.         } if (navigator.geolocation) {   
  95.            navigator.geolocation.getCurrentPosition(success, error);  
  96.            } else {   
  97.            error(‘not supported‘);  
  98.            }   
  99.            </script>   
  100. </body>  
  101. </html>  

 

技术分享

 

技术分享

利用html5 共享地理位置

标签:

原文地址:http://www.cnblogs.com/jiaze/p/5061536.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!