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.