HTML5 geolocation API obtains the user's current geographic location

Source: Internet
Author: User

The geolocation API is introduced in HTML5 to help users obtain the geographic location of the browser, which not only indicates the current latitude and longitude, it can also be used with the google map API to mark the current location on the map.

The HTML code is mainly two blocks. The first part of the container is used to put google Maps and mark locations, and the second part is used to display precise latitude and longitude and accuracy errors ).

 
 
  1. <! Doctype html>
  2. <Html>
  3. <Head>
  4. <! -- The following code is officially recommended by googlemap, which allows users to disable the use of the browser's zoom-in and zoom-out window function while using the googlemap's own zoom->
  5. <Meta name = "viewport" content = "initial-scale = 1.0, user-scalable = no"/>
  6. <Title> use Google Map on the page to obtain the current location of the browser </title>
  7.  
  8. <! -- Because google map is used on the page, import the googlemap file. -->
  9. <Script type = "text/javascript" src = "http://maps.google.com/maps/api/js? Sensor = false "> </script>
  10.  
  11. <! -- This js file was written by myself. It is used to obtain the current location using the HTML5 geolocation API and identified on google map -->
  12. <Script type = "text/javascript" src = "js/googlemap. js"> </script>
  13. </Head>
  14.  
  15. <Body onload = "init ()">
  16. Hello, Google Map!
  17. <Br>
  18. <! -- This square area is used to draw a google Map -->
  19. <Div id = "map" style = "width: 400px; height: 400px"> </div>
  20. <Br>
  21. <! -- This area is used to display your location information -->
  22. <H3> your browser displays your current location information:
  23. <P id = "positionInfo"> </p>
  24. </Body>
  25.  
  26. </Html>

The javascript method is mainly used to draw a map and mark the location), and provides the callback function definition when the location is obtained successfully or fails:

 
 
  1. // This is the initialization method used to draw a google Map
  2. Function init (){
  3. Console. log ("entering the init () method ");
  4. // First, you must determine whether the browser has the geolocation attribute. Because HTML5 adds this attribute, not all browsers support this attribute.
  5. If (navigator. geolocation ){
  6. // If the browser supports geolocation, use the getCurrentLocation method of geolocation to obtain the current geographical location of the user,
  7. // Call the show_map () callback function after successful acquisition
  8. Console. log ('browser support geolocation ');
  9. Navigator. geolocation. getCurrentPosition (show_map, handle_error, null );
  10. } Else {
  11. Console. log ('browser doesnt support geolocation ');
  12. }
  13.  
  14. }
  15.  
  16.  
  17. // This is a callback function used to respond when geolocation successfully obtains the geographical location of the user's browser. It encapsulates all the location information in position.
  18. // Therefore, we need to parse the position to obtain the user's detailed information.
  19. Function show_map (position ){
  20.  
  21. // Obtain the current geographic location
  22. Var coords = position. coords;
  23. // Part 1; displays the user's precise location information
  24. // Component used to display precise location information on the page
  25. Var positionInfo = document. getElementById ("positionInfo ");
  26. Var positionString = "longitude:" + coords. longpolling + "<br> ";
  27. PositionString + = "dimension:" + coords. latitude + "<br> ";
  28. Var altitude = coords. altitude;
  29. If (altitude! = Null ){
  30. PositionString + = "altitude" + coords. altitude + "<br> ";
  31. }
  32. PositionString + = "longitude and latitude:" + coords. accuracy + "meters" + "<br> ";
  33. PositionInfo. innerHTML = positionString;
  34. // Part 2; display the current location of the browser on google Map
  35. // Set the map parameters to set the dimensions and precision of the current location to the central point of the map.
  36. Var latlng = new google. maps. LatLng (coords. latitude, coords. longpolling );
  37. Var myOptions = {
  38. // Set the magnification.
  39. Zoom: 14,
  40. // Set the center point of the map to the specified coordinate point
  41. Center: latlng,
  42. // Specify the map type. The street map is selected here.
  43. MapTypeId: google. maps. MapTypeId. ROADMAP
  44. };
  45. // Create a map and display it in "map" div. This map is called map1.
  46. Var map1;
  47. Map1 = new google. maps. Map (document. getElementById ("map"), myOptions );
  48. // Create a tag on the map
  49. Var marker = new google. maps. Marker ({
  50. // Mark the created annotation point. Because the annotation point is set by the current longitude and latitude, it indicates the current position.
  51. Position: latlng,
  52. // On which map is marked, map1 is created as google map, so it is marked on map1.
  53. Map: map1
  54. });
  55. // Set the annotation window and specify the comments of the window
  56. Var infowindow = new google. maps. InfoWindow ({
  57. Content: "This is the current location of Charles's browser! "
  58. });
  59. // Open the annotation window
  60. InfoWindow. open (map1, marker );
  61.  
  62. }
  63.  
  64.  
  65.  
  66. // This is the second callback function. It is used to obtain the response when the geolocation fails to get the geographical location of the user's browser.
  67. // The error object encapsulates all possible errors that cannot be obtained from a geographical location, and HTML5 reserves an error code for it. The value can be {1, 2, 3}
  68. Function handle_error (error ){
  69. Var errorTypes = {
  70. 1: 'location service denied ',
  71. 2: 'location information not obtained ',
  72. 3: 'Information retrieval timeout'
  73. };
  74. Console. log (errorTypes [error. code] + ":, your current geographic location cannot be determined ");
  75. }

 

To display the results, you must enable the shared location function because it is private. I tested Opera 11 and Firefox 10, and Google Chrome.

Starting from Opera 11, you need to select whether to share the geographical location:

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1324121A8-0.png "/>

Then sign a User Agreement:

 

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1324122F1-1.png "/>

 

For Firefox, you also need to get a permission to share your location:

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/132412E93-2.png "/>

 

 

In the end, the user's current location is actually exposed by your ip). For example, if I live near the guanglan Road Subway Station in Shanghai, the result is as follows:

 

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/132412KK-3.png "/>

 

This article from "parallel line cohesion" blog, please be sure to keep this source http://supercharles888.blog.51cto.com/609344/856656

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.