位置并没有出来

时间:2022-04-06 06:21:15

1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"/> 6 <title></title> 7 <script language="javascript" src="http://webapi.amap.com/maps?v=1.3&amp;key=e8496e8ac4b0f01100b98da5bde96597"></script> 8 <script src="mAmaplbs.js"></script> 9 </head> 10 <body> 11 12 <a id="distance" onclick="getDistance()">获取距离</a> 13 <script> 14 //获取当前位置(要领名) 15 mMap.getSessionLocation(locationFunc) 16 function locationFunc(){ 17 var data = JSON.parse(sessionStorage.getItem("location")); 18 console.log(data); 19 alert("lng:"+data.position.lng) 20 alert("lat:"+data.position.lat) 21 } 22 23 // 获取两点的距离 (m) 24 function getDistance(){ 25 var obj1={lng:116.39,lat: 39.98}; 26 var obj2={lng:116.39,lat: 38.98}; 27 alert(mMap.distance(obj1,obj2)); 28 mMap.serverDistance(obj1,obj2); 29 } 30 </script> 31 </body> 32 </html>

mAmaplbs.js

//用户位置定位 使用geolocation定位 var mMap=function(){ function rad(d){ return d*Math.PI/180.0; } this.map={}, this.geolocation={}, this.k=0, //加载舆图,挪用浏览器定位处事 this.initMap=function(mapContainer,completFunc){ if(typeof(AMap)=="object"){ this.map = new AMap.Map(mapContainer, { resizeEnable: true }); this.map.plugin(‘AMap.Geolocation‘, function () { this.geolocation = new AMap.Geolocation({ enableHighAccuracy: true,//是否使用高精度定位,默认:true timeout: 10000, //赶过10秒后遏制定位,默认:无穷大 maximumAge: 0, //定位功效缓存0毫秒,,默认:0 convert: true, //自动偏移坐标,偏移后的坐标为高德坐标,默认:true showButton: true, //显示定位按钮,默认:true buttonPosition: ‘LB‘, //定位按钮停靠位置,默认:‘LB‘,左下角 buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20) showMarker: true, //定位告成后在定位到的位置显示点符号,默认:true showCircle: true, //定位告成后用圆圈暗示定位精度范畴,默认:true panToLocation: true, //定位告成后将定位到的位置作为舆图中心点,默认:true zoomToAccuracy:true //定位告成后调解舆图视野范畴使定位位置及精度范畴视野内可见,默认:false }); this.map.addControl(this.geolocation); AMap.event.addListener(this.geolocation, ‘complete‘, onComplete);//返回定位信息 AMap.event.addListener(this.geolocation, ‘error‘, onError); //返回定位堕落信息 }); function onComplete(data){ console.log(completFunc) console.log(data) if(completFunc){ completFunc(data); } } function onError(){ var str = ‘定位掉败,‘; str += ‘错误信息:‘ switch(data.info) { case ‘PERMISSION_DENIED‘: str += ‘浏览器阻止了定位操纵‘; break; case ‘POSITION_UNAVAILBLE‘: str += ‘无法获恰当前位置‘; break; case ‘TIMEOUT‘: str += ‘定位超时‘; break; default: str += ‘未知错误‘; break; } alert(str) } } }, this.getCurrentPosition=function(callback){ if(typeof(this.geolocation.getCurrentPosition)!=‘undefined‘){ this.geolocation.getCurrentPosition(); }else{ setTimeout(function(){ //将获得的经纬度信息,放入sessionStorge this.getSessionLocation(callback) },200) } }, this.distance = function(obj1,obj2){//return:m var lng=new AMap.LngLat(obj1.lng, obj1.lat); var lag=new AMap.LngLat(obj2.lng, obj2.lat); var ss=lng.distance(lag); return ss; }, this.getSessionLocation=function(callback){ if(sessionStorage.getItem(‘location‘)){ callback(); }else{ this.initMap(‘‘,function(data){ sessionStorage.setItem("location",JSON.stringify(data)) callback(); }); this.getCurrentPosition(callback); } }, /* *两点之间的距离 *(lng1.lat1)地点一的经纬度 *(lng2.lat2)地点一的经纬度 *单位米 */ this.serverDistance = function(obj1,obj2){//return:m var radLat1 = rad(obj1.lat); var radLat2 = rad(obj2.lat); var a = radLat1 - radLat2; var b = rad(obj1.lng)- rad(obj2.lng); var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2))); s = s *6378137; s = Math.round(s * 10000)/10000 ; return s; } return this; }();