H5+百度地图自定义定位控件、调用H5+定位接口

由于百度地图的定位控件是调用内置的定位接口,想要调用H5+的定位接口就不行,自己改造一下就好了


/**
 * Author 岳晓
 * 
 * 自定义定位控件,调用H5+内置定位API
 */

(function(BMap){
	
	var control = BMap.Map.XGeolocationControl = function(){
		this.defaultAnchor = BMAP_ANCHOR_BOTTOM_LEFT;
		this.defaultOffset = new BMap.Size(10, 50);
	}
	
	control.prototype = new BMap.Control();
	
	control.prototype.locate=function(cb){
		_locate(this.map,this.controlPane.querySelector(".BMap_geolocationContainer"),cb);
		
	};
	control.prototype.getMap=function(){
		return this.map;
	};
	control.prototype.setOnLocateComplete=function(fn){
		if(typeof(fn)=="function"){
			this.locateComplete = fn;
		}
	};
	control.prototype.initialize = function(map){
		this.map = map;
		var div = document.createElement("div");
      	
	  	div.innerHTML=
	  		'<div class="BMap_geolocationIconBackground" style="height: 32px; background-image: url(http://api0.map.bdimg.com/images/geolocation-control/mobile/gradient-bg-1x64.png); background-size: 1px 32px; background-repeat: repeat-x;">'+
	  			'<div class="BMap_geolocationIcon" style="width: 32px; height: 32px; cursor: pointer; background-image: url(http://api0.map.bdimg.com/images/geolocation-control/mobile/fail-40x40.png); background-size: 20px 20px; background-position: 50% 50%; background-repeat: no-repeat;"></div>'+
	  		'</div>'+
	  		'<div class="BMap_geolocationAddress" style="padding:0 10px 0 10px;background:#FFF;display:-webkit-box;-webkit-box-align:center; box-sizing:border-box;border-left-width: 1px; border-left-style: solid; border-left-color: rgb(217, 215, 213); background-image: url(http://api0.map.bdimg.com/images/geolocation-control/mobile/gradient-bg-1x64.png); background-size: 1px 32px; background-repeat: repeat-x;">'+
	  		'	<div class="BMap_geolocationAddressText" style="text-align:center;font-size: 12px; color: #666666; text-overflow: ellipsis; white-space: nowrap; overflow: hidden; display: block; min-width: 50px; max-width: 400px;"></div>'+
	  		'</div>';
	 	div.className = "BMap_geolocationContainer";
      	div.setAttribute("style",'-webkit-transition:width 0.2s ease-in;height: 32px;;width:34px;display: -webkit-box; margin: 0px; box-sizing: border-box; border: 1px solid #d9d7d5; border-radius: 3px; -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,.2); overflow: hidden;')
	  	var geolocation = div.querySelector(".BMap_geolocationIcon");
	  	var address = div.querySelector(".BMap_geolocationAddress");
	  	var text = div.querySelector(".BMap_geolocationAddressText");
	  	map.getContainer().appendChild(div);
		var $this = this;
		geolocation.addEventListener("click", function(e){
			_locate(map,div,$this.locateComplete);
	 	});
	 	this.controlPane = div.parentElement;
	  	return div;
	};
	
	var mk = null;
	function _locate(map,div,cb){
		
		if(mk){
			map.removeOverlay(mk);
			mk.disableMassClear();
		} 
		
		
		var geolocation = div.querySelector(".BMap_geolocationIcon");
	  	var address = div.querySelector(".BMap_geolocationAddress");
	  	var text = div.querySelector(".BMap_geolocationAddressText");
		
		div.style.width= "34px";
		text.innerText="";
		geolocation.style.backgroundImage = "url(http://api0.map.bdimg.com/images/geolocation-control/mobile/loading-40x40.gif)";
		
		Towery.locate({
			success:function(position){
				if(cb) cb({
					status:0,
					result:position
				});
				geolocation.style.backgroundImage = "url(http://api0.map.bdimg.com/images/geolocation-control/mobile/success-40x40.png)";

				var coords = position.coords;
				var lon = coords.longitude;
				var lat = coords.latitude;
				var point = new BMap.Point(lon,lat);
					
				var myIcon = new BMap.Icon(Towery.getFullPath("MobileCRMDistrict/images/location-ico.svg"),new BMap.Size(16, 16));
				
				mk = new BMap.Marker(point,{icon:myIcon});
				var label = new BMap.Label("我在这里",{
					offset:new BMap.Size(-18,-20)
				});
				
				label.setStyle({
					padding:"0px",
					border:"solid 1px orangered",
					background:"rgba(255,255,255,0.5)"
				});
				mk.setTop(true);
				mk.setLabel(label);
				map.addOverlay(mk);
				map.panTo(point);
				
				if(position.addresses){
					text.innerText=position.addresses;
					var w = text.offsetWidth;
					div.style.width=34 +(w +20)+"px" ;
				}
			},
			error:function(e){
				if(cb) cb({
					status:-1,
					result:e
				});
				plus.nativeUI.toast("无法获取您的位置,请打开GPS并授予此应用权限,必要时移至室外或移动、WIFI信号好的位置!",{duration:"long"});
				geolocation.style.backgroundImage = "url(http://api0.map.bdimg.com/images/geolocation-control/mobile/fail-40x40.png)";
				console.log(Towery.pretty(e))
			}
		},{
			provider: "baidu",
			coordsType:"bd09ll"
		});	
	}
	
})(BMap);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值