Share your location with HTML5

Source: Internet
Author: User

This example demonstrates the use of HTML5 for geo-positioning . That is, you get the native IP address, and then you know where it is, and it's displayed on Google Maps ...

Instance

The instance preview address: Http://html5demos.com/geo

xml/html Code copy content to clipboard
  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><a 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 does 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>

Share your location with HTML5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.