//单纯的获取,不应用到地图上 var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ console.log(JSON.stringify(r)); }else { console.log('failed'+this.getStatus()); } }); //应用到地图上 var map = new BMap.Map("allmap"); //allmap为显示地图容器的ID var point = new BMap.Point(116.331398,39.897445); map.centerAndZoom(point,12); var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ var mk = new BMap.Marker(r.point); map.addOverlay(mk); map.panTo(r.point); }else { console.log('failed'+this.getStatus()); } });下面是html5 plus获取经纬度的方法:
注:html5获取的经纬度是GPS坐标,不能直接用在百度地图上,需要转换成百度坐标,即BD-09。上面的代码已经转换过了。
mui.plusReady(function(){ var curLat = plus.webview.currentWebview().lat; var curLon = plus.webview.currentWebview().long; initMap(curLat,curLon); }) function initMap(curLat,curLon){ //将传递的坐标转为百度坐标 var bdPos = bd_encrypt(curLon, curLat); var point = new BMap.Point(bdPos.bd_lon, bdPos.bd_lat); //创建点坐标 //设置百度地图 var map = new BMap.Map('map'); //map为显示百度地图的容器的ID map.centerAndZoom(point,18); //设置地图中心 //自定义图标 var icon = new BMap.Icon('../images/bdmap_marker.png', new BMap.Size(66,47), { anchor: new BMap.Size(33,33) }); var marker = new BMap.Marker(point, { icon: icon }); map.addOverlay(marker); //加入覆盖物 } //火星坐标转百度坐标 function bd_encrypt(gg_lon, gg_lat){ var X_PI = Math.PI * 3000.0 / 180.0; var x = gg_lon, y = gg_lat; var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI); var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI); var bd_lon = z * Math.cos(theta) + 0.0065; var bd_lat = z * Math.sin(theta) + 0.006; return { bd_lat: bd_lat, bd_lon: bd_lon }; }
部分素材资源来源网站,本站提供免费下载,如有侵权请联系站长马上删除!