This article mainly introduces how to obtain information about the current location based on mobile phone location based on JavaScript (No. X, district X, city X, district X, if you need a friend, you can refer to the following nonsense and paste the js Code directly. The Code is as follows:
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 location information object") ;}; var self = this; if (navigator. geolocation) {// the browser supports geolocation navigator. geolocation. getCurrentPosition (function (position) {// longitude var longpolling = position. coords. longpolling; // latitude var latitude = position. coords. latitude; self. loadMapApi (longpolling, latitude) ;}, self. onError, options) ;}else {// the browser does not support geolocation }}; Location. prototype. loadMapApi = function (longpolling, 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 (longpolling, 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 rejected"); break; case 2: alert ("location information temporarily unavailable"); break; case 3: alert ("Get information timeout"); break; case 4: alert ("Unknown error"); break ;}; // call var local = new Location (); local. getLocation (function (res) {// here is the returned geographic location information console. log (res );})
The above content is based on JavaScript that I want to introduce to you to obtain the current Location Based on Mobile Phone Positioning (No. X, street X, county X, city X, Province X). I hope you will like it.