HTML Location City

Source: Internet
Author: User

    1. <html>
    2. <head>
    3. <meta charset="UTF-8" />
    4. <title>js Baidu geolocation positioning the current city </title>
    5. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 " />
    6. <Meta name="viewport" content="initial-scale=1.0, User-scalable=no" />
    7. <script src="Http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script >
    8. <script src="Http://api.map.baidu.com/api?v=2.0&ak=abcs"></Script >
    9. <script type="Text/javascript">
    10. var geolocation = new Bmap.geolocation ();
    11. var gc = new Bmap.geocoder ();
    12. Geolocation.getcurrentposition (function (r) {//Anchor result object is passed to r variable
    13. if (this.getstatus () = = bmap_status_success)
    14. {//through the Geolocation class of GetStatus () can determine whether the successful positioning.
    15. var pt = R.point;
    16. Gc.getlocation (PT, function (RS) {
    17. var addcomp = rs.addresscomponents;
    18. Alert (addcomp.province + addcomp.city + addcomp.district + addcomp.street + addcomp.streetnumber);
    19. });
    20. }
    21. Else
    22. {
    23. About Status Codes
    24. Bmap_status_success retrieved successfully. Corresponds to the value "0".
    25. Bmap_status_city_list City list. Corresponds to the value "1".
    26. Bmap_status_unknown_location location result unknown. Corresponds to the value "2".
    27. Bmap_status_unknown_route navigation results are unknown. Corresponds to the value "3".
    28. Bmap_status_invalid_key illegal keys. Corresponds to the value "4".
    29. Bmap_status_invalid_request illegal request. Corresponds to the value "5".
    30. Bmap_status_permission_denied does not have permissions. Corresponds to the value "6". (added since 1.1)
    31. The Bmap_status_service_unavailable service is not available. Corresponds to the value "7". (added since 1.1)
    32. Bmap_status_timeout timeout. Corresponds to the value "8". (added since 1.1)
    33. Switch (This.getstatus ())
    34. {
    35. Case 2:
    36. Alert (' Position result unknown ' get position failed. ');
    37. Break
    38. Case 3:
    39. Alert (' Navigation result unknown ' Get location failed: ');
    40. Break
    41. Case 4:
    42. Alert (' Illegal key acquisition location failed. ');
    43. Break
    44. Case 5:
    45. Alert (' Sorry, illegal request location failed to get location. ');
    46. Break
    47. Case 6:
    48. Alert (' Sorry, there is currently no permission to get the location failed. ');
    49. Break
    50. Case 7:
    51. Alert (' Sorry, the service is not available to get the location failed. ');
    52. Break
    53. Case 8:
    54. Alert (' Sorry, the request timed out to get the location failed. ');
    55. Break
    56. }
    57. }
    58. },
    59. {Enablehighaccuracy:true}
    60. )
    61. </Script>
    62. </head>
    63. <body>
    64. </body>
    65. </html>

Additional HTML5 geolocation positioning (get current latitude and longitude):

  1. <html>
  2. <head>
  3. <meta charset="UTF-8" />
  4. <title>js geolocation positioning the current city </title>
  5. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 " />
  6. <Meta name="viewport" content="initial-scale=1.0, User-scalable=no" />
  7. <script type="Text/javascript">
  8. if (navigator.geolocation)
  9. {
  10. Navigator.geolocation.getCurrentPosition (
  11. function (position) {
  12. Show_msg (position);
  13. },
  14. function (Err) {
  15. Switch (Error.code)
  16. {
  17. Case 0:
  18. Alert ("An error occurred while trying to get your location information:" + error.message);
  19. Break
  20. Case 1:
  21. Alert ("The user denied obtaining a location information request.") ");
  22. Break
  23. Case 2:
  24. Alert ("The browser cannot get your location information:" + error.message);
  25. Break
  26. Case 3:
  27. Alert ("Get your location information timed out. ");
  28. Break
  29. }
  30. }
  31. )
  32. }
  33. Else
  34. Alert (' The browser you are currently using does not support geolocation services ');
  35. function show_msg (position)
  36. {
  37. var lat = position.coords.latitude;
  38. var lng = position.coords.longitude;
  39. Alert ("Your location: Longitude" + lat + ", latitude" + LNG);
  40. if (typeof position.address! = "undefined")
  41. {
  42. var country = position.address.country;
  43. var province = position.address.region;
  44. var City = position.address.city;
  45. Alert (' You are located in ' + country + province + ' province ' + City + ' municipality ');
  46. }
  47. }
  48. </Script>
  49. </head>
  50. <body>
  51. <p id="Geo_loc"><p>
  52. </body>
  53. </html>

HTML Location City

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.