废话不多说了,直接给大家贴js代码了。具体代码如下所示:
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) 可获得定位信息对象); }; var self = this; if (navigator.geolocation) { //浏览器支持geolocation navigator.geolocation.getcurrentposition(function(position){ //经度 var longitude = position.coords.longitude; //纬度 var latitude = position.coords.latitude; self.loadmapapi(longitude,latitude); }, self.onerror, options); } else { //浏览器不支持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(位置服务被拒绝); break; case 2: alert(暂时获取不到位置信息); break; case 3: alert(获取信息超时); break; case 4: alert(未知错误); break; }};//调用var local = new location();local.getlocation(function(res){ //此处就是返回的地理位置信息 console.log(res);})
以上内容是小逼给大家介绍的基于javascript实现根据手机定位获取当前具体位置(x省x市x县x街道x号),希望大家喜欢。