How to call Baidu Map API to realize mobile phone automatic location _android

Source: Internet
Author: User

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>

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.