- <html>
- <head>
- <meta charset="UTF-8" />
- <title>js Baidu geolocation positioning the current city </title>
- <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 " />
- <Meta name="viewport" content="initial-scale=1.0, User-scalable=no" />
- <script src="Http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script >
- <script src="Http://api.map.baidu.com/api?v=2.0&ak=abcs"></Script >
- <script type="Text/javascript">
- var geolocation = new Bmap.geolocation ();
- var gc = new Bmap.geocoder ();
- Geolocation.getcurrentposition (function (r) {//Anchor result object is passed to r variable
- if (this.getstatus () = = bmap_status_success)
- {//through the Geolocation class of GetStatus () can determine whether the successful positioning.
- var pt = R.point;
- Gc.getlocation (PT, function (RS) {
- var addcomp = rs.addresscomponents;
- Alert (addcomp.province + addcomp.city + addcomp.district + addcomp.street + addcomp.streetnumber);
- });
- }
- Else
- {
- About Status Codes
- Bmap_status_success retrieved successfully. Corresponds to the value "0".
- Bmap_status_city_list City list. Corresponds to the value "1".
- Bmap_status_unknown_location location result unknown. Corresponds to the value "2".
- Bmap_status_unknown_route navigation results are unknown. Corresponds to the value "3".
- Bmap_status_invalid_key illegal keys. Corresponds to the value "4".
- Bmap_status_invalid_request illegal request. Corresponds to the value "5".
- Bmap_status_permission_denied does not have permissions. Corresponds to the value "6". (added since 1.1)
- The Bmap_status_service_unavailable service is not available. Corresponds to the value "7". (added since 1.1)
- Bmap_status_timeout timeout. Corresponds to the value "8". (added since 1.1)
- Switch (This.getstatus ())
- {
- Case 2:
- Alert (' Position result unknown ' get position failed. ');
- Break
- Case 3:
- Alert (' Navigation result unknown ' Get location failed: ');
- Break
- Case 4:
- Alert (' Illegal key acquisition location failed. ');
- Break
- Case 5:
- Alert (' Sorry, illegal request location failed to get location. ');
- Break
- Case 6:
- Alert (' Sorry, there is currently no permission to get the location failed. ');
- Break
- Case 7:
- Alert (' Sorry, the service is not available to get the location failed. ');
- Break
- Case 8:
- Alert (' Sorry, the request timed out to get the location failed. ');
- Break
- }
- }
- },
- {Enablehighaccuracy:true}
- )
- </Script>
- </head>
- <body>
- </body>
- </html>
Additional HTML5 geolocation positioning (get current latitude and longitude):
- <html>
- <head>
- <meta charset="UTF-8" />
- <title>js geolocation positioning the current city </title>
- <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 " />
- <Meta name="viewport" content="initial-scale=1.0, User-scalable=no" />
- <script type="Text/javascript">
- if (navigator.geolocation)
- {
- Navigator.geolocation.getCurrentPosition (
- function (position) {
- Show_msg (position);
- },
- function (Err) {
- Switch (Error.code)
- {
- Case 0:
- Alert ("An error occurred while trying to get your location information:" + error.message);
- Break
- Case 1:
- Alert ("The user denied obtaining a location information request.") ");
- Break
- Case 2:
- Alert ("The browser cannot get your location information:" + error.message);
- Break
- Case 3:
- Alert ("Get your location information timed out. ");
- Break
- }
- }
- )
- }
- Else
- Alert (' The browser you are currently using does not support geolocation services ');
- function show_msg (position)
- {
- var lat = position.coords.latitude;
- var lng = position.coords.longitude;
- Alert ("Your location: Longitude" + lat + ", latitude" + LNG);
- if (typeof position.address! = "undefined")
- {
- var country = position.address.country;
- var province = position.address.region;
- var City = position.address.city;
- Alert (' You are located in ' + country + province + ' province ' + City + ' municipality ');
- }
- }
- </Script>
- </head>
- <body>
- <p id="Geo_loc"><p>
- </body>
- </html>
HTML Location City