Js uses Baidu map to locate and obtain longitude and latitude

Source: Internet
Author: User

The positioning function needs to be used in recent projects, and the longitude and latitude of the current position are obtained. The following code is compiled for reference:

Function locationPosition (){
Var geolocation = new BMap. Geolocation ();
Geolocation. getCurrentPosition (function (r ){
 
Var city_lat = r. latitude;
Var city_lng = r. Longpolling;
 
Cookies. set ("save_city_latitude", city_lat );
Cookies. set ("save_city_long.pdf", city_lng );
// Console. log (city_lat );
// Console. log (city_lng );
If (this. getStatus () = BMAP_STATUS_SUCCESS ){
Var geoc = new BMap. Geocoder ();
Var pt = r. point;
Geoc. getLocation (pt, function (rs ){
// Obtain the longitude and latitude
 
Var addComp = rs. addressComponents;
 
// Alert (addComp. province + "," + addComp. city + "," + addComp. district + "," + addComp. street + "," + addComp. streetNumber );
$ City = $ (". head-left> a> span ");
// Var city_name = $. fn. cookie ('location _ City ');
Var city_name = Cookies. get ('location _ City ');
// Console. log (addComp. city );
If (city_name = addComp. city ){
// If the location of the TODO city is the same as the cookie value, no operation is performed.
$ City. text (addComp. city );
$ (". City-now"). text (addComp. city );
} Else {
$ City. text (city_name );
If (confirm ('the current location is different from the location of the target city, are you sure you want to change it to the current location? ')){
$ City. text (addComp. city );
$ (". City-now"). text (addComp. city );
// $. Fn. cookie ('location _ City', addComp. city );
// Cookies. set ('location _ City', addComp. city );
// SetLocation ();
}
}
 
Var city = Cookies. set ('location _ City', addComp. city );
 
Currount_city_name = addComp. city;
 
GetData (1, city_lat, city_lng, currount_city_name );
GetArea (currount_city_name );
 
});
}
Else {
Alert ('locating failed ');
 
}
 
},{ EnableHighAccuracy: true });
 
}
 
LocationPosition ();

Where:

Var city_lat = r. latitude;
Var city_lng = r. Longpolling;
You can obtain the current longitude and latitude.

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.