Based on JavaScript implementation based on mobile phone positioning to obtain the current location (x County x Street x) _javascript tips

Source: Internet
Author: User

Nonsense don't say much, directly to everyone paste JS code. The specific code looks like this:

function Location () {};
  Location.prototype.getLocation = function (callback) {var options = {enablehighaccuracy:true, maximumage:1000
  };
    This.callback = Object.prototype.toString.call (callback) = = "[Object Function]"?
      Callback:function (address) {alert (address.province + address.city);
    Console.log ("Getocation (callbackfunction) can obtain the positioning Information object");
  };
  var self = this;
      The IF (navigator.geolocation) {//browser supports geolocation navigator.geolocation.getCurrentPosition (function (position) {
      Longitude var longitude = position.coords.longitude;
      Latitude var latitude = position.coords.latitude;
    Self.loadmapapi (Longitude,latitude);
  }, Self.onerror, options);
else {//browser does not support geolocation}};
  Location.prototype.loadMapApi = function (longitude, latitude) {var self = this;
  var ohead = document.getElementsByTagName (' head '). Item (0);
  var oscript= document.createelement ("script");
  Oscript.type = "Text/javascript"; Oscript.src= "http://api.map.baidu.com/getscript?v=2.0&ak=A396783ee700cfdb9ba1df281ce36862&services=&t=
  20140930184510 ";
  Ohead.appendchild (Oscript);
    Oscript.onload = function (date) {var point = new Bmap.point (longitude, latitude);
    var gc = new Bmap.geocoder ();
      Gc.getlocation (Point, Function (RS) {var addcomp = rs.addresscomponents;
    Self.callback (Addcomp);
  });
}
};
      Location.prototype.onError = function (Error) {switch (error.code) {case 1:alert ("Location service denied");
    Break
      Case 2:alert ("temporary access to location information");
    Break
      Case 3:alert ("Get information timed out");
    Break
      Case 4:alert ("Unknown error");
  Break
}
};
Call var local = new Location (); Local.getlocation (function (res) {//Here is the geographic information returned Console.log (res);})

The above is a small force to introduce the implementation of JavaScript based on the mobile phone location to obtain the current specific location (x province x, x County x Street x), I hope you like.

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.