Recently because of the project needs, the study of the next Baidu map positioning, they provide an example of the basic use of listeners to achieve automatic positioning. I want to achieve an effect: when the user enters the UI, does not locate, the user needs to locate the time, oneself manually clicks the button, then goes to locate the current position. After 2 days of research and consultation, found a solution, this memo.
Declare address resolver var GEOC = new Bmap.geocoder (); Auto-locate var autolocation = function () {if (navigator.geolocation) {//Determine if the browser can get the current position Navigator.geolocation.getCurre
Ntposition (ADDRSUC, Addrfail);
else {Simplenoty ("cannot be automatically located, please enter your dining address");
}//Get current coordinates success function addrsuc (param) {var lng = param.coords.longitude;
var lat = param.coords.latitude;
var point = new Bmap.point (LNG, LAT);
Convert GPS coordinates to Baidu address coordinates BMap.Convertor.translate (point, 0, Translatecallback);
}//Get coordinate failed function Addrfail (err) {shownotify ("auto-locate Failed");} Coordinate conversion function Translatecallback (point) {geoc.getlocation (point, Function (RS) {var addcomp = Rs.addresscompo nents;
Query-obtained Address object component//addcomp.streetnumber: Street number//addcomp.city: City//addcomp.district: District//addcomp.street: Street
Addcomp.province: Province var address = addcomp.city + addcomp.district + addcomp.street; var lng_t = point.lng; Longitude var lat_t = Point.lat; Latitude $ (". Locationtxt"). Text ("Current position:" + address);
})
}
Object Example:
Rs object: {point:h, Address: "Guang Shun bei da Jie, Chaoyang District, Beijing, Addresscomponents:object, Surroundingpois:array[0], business:" Wangjing, to Canton Camp, Flower Home "}
Addresscomponents object: {streetnumber: "", Street: "Guang Shun bei da Jie", District: "Chaoyang District", City: "Beijing", Province: "Beijing"}
Call:
$ (function () {
aotulocation ();
}); <br><br>