<template>
	<view>
		<view id="container" :style="myStyle"></view>
		
	</view>
</template>

<script>
		import MapLoader from '@/utils/AMap'
	export default {
		name:"Carmap",
		props:{
			myStyle: {
				require: false,
				default: "width: 100%; height: 240px;",
			},
		},
		data() {
			return {
				longitude: 112.276527,
				latitude: 30.306427,
				AMap:null,
				//车的位置
				car: {
					obj:null,
					longitude: '',
					latitude: '',
				},
				down: {
					obj:null,
					longitude: '',
					latitude: '',
				},
				up: {
					obj:null,
					longitude: '',
					latitude: '',
				},
				//个人
				info: {
					obj:null,
					init: false,
					longitude: '',
					latitude: '',
				},
				mapbus:null,
				startend: [{
					longitude:112.279274,
					latitude:30.303273,
					name:''
				}, {
					longitude:112.273867,
					latitude:30.309817,
					name:''
				}], 
				start:{},
				end:{},
				covers: [
					{
						longitude:112.273867,
						latitude:30.309817,
					},{
						longitude:112.280261,
						latitude:30.3041,
					},{
						longitude:112.279274,
						latitude:30.303273,
					}
				]
			};
		},
		methods:{
			setStartend(sz){
				this.start.setPosition( [sz[0].longitude,sz[0].latitude]);
			this.end.setPosition( [sz[sz.length-1].longitude,sz[sz.length-1].latitude]);
			
			},
			setCenter(item){
				if(this.mapbus){
					this.mapbus.setCenter([item.longitude,item.latitude]);
				}
			},
			setDown(ob){
				if(this.AMap==null){
					return
				}
				this.down.longitude=ob.longitude
				this.down.latitude=ob.latitude
				if(!this.down.ob){
					
					var icon0 = require("@/static/img/down.png");
					
					this.down.ob= new AMap.Marker({
						map: this.mapbus,
						position: [this.down.longitude,this.down.latitude],
						offset: new AMap.Pixel(-15, -30),
						content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
						
						zIndex:999,
						autoRotation: true,
					});
					
				}else{
					this.down.ob.setPosition( [this.down.longitude,this.down.latitude]);
				}
			},
			setUp(ob){
				if(this.AMap==null){
					return
				}
				this.up.longitude=ob.longitude
				this.up.latitude=ob.latitude
				if(!this.up.ob){
					
					var icon0 = require("@/static/img/up.png");
					
					this.up.ob= new AMap.Marker({
						map: this.mapbus,
						position: [this.up.longitude,this.up.latitude],
						offset: new AMap.Pixel(-15, -30),
						
						
						content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
						
						zIndex:999,
						autoRotation: true,
					});
					
				}else{
					this.up.ob.setPosition( [this.up.longitude,this.up.latitude]);
				}
			},
			setCar(ob){
				if(this.AMap==null){
					return
				}
				 this.car.longitude=ob.longitude
				 this.car.latitude=ob.latitude
				 if(!this.car.ob){
				 	
				 	var icon0 = require("@/static/img/icon-busPosition.png");
				 	
				 	this.car.ob= new AMap.Marker({
				 		map: this.mapbus,
				 		position: [this.car.longitude,this.car.latitude],
				 		offset: new AMap.Pixel(-5, -5),
				 		
				 		content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
				 		
				 		zIndex:99,
				 		autoRotation: true,
				 	});
				 	
				 }else{
				 	this.car.ob.setPosition( [this.car.longitude,this.car.latitude]);
				 }
			 
				
			},
			setPerson(ob){
				if(this.AMap==null){
					return
				}
				
					this.info.longitude=ob.longitude
					this.info.latitude=ob.latitude
					if(!this.info.ob){
						
						var icon0 = require("@/static/img/icon-userPosition.png");
						
						
						this.info.ob= new AMap.Marker({
							map: this.mapbus,
							position: [this.info.longitude,this.info.latitude],
							offset: new AMap.Pixel(-5, -5),
							content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
							
							zIndex:99,
							autoRotation: true,
						});
						//this.mapbus.setCenter([this.info.longitude,this.info.latitude]); //设置地图中心点
						this.mapbus.setCenter([ this.info.longitude,this.info.latitude]); //设置地图中心点

					}else{
						this.info.ob.setPosition( [this.info.longitude,this.info.latitude]);
					}
				
				
			},
			initMap(point,stationList,startend){
				
				var _this = this;
				this.covers=point;
				this.startend=startend;
				MapLoader().then(AMap => {
					
					this.AMap=AMap;
					_this.mapbus && _this.mapbus.destroy();
				
					_this.mapbus=new AMap.Map("container", {
						resizeEnable: true,
						center: [this.longitude, this.latitude],
						zoom: 14
					});
					var lineArr=[]
					var icon0 = require("@/static/img/map_0.png");
					var icon1 = require("@/static/img/map_1.png");
					var icon2 = require("@/static/img/map_2.png");
					this.start=new AMap.Marker({
						map: _this.mapbus,
						position: [_this.startend[0].longitude,_this.startend[0].latitude],
						content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
						offset: new AMap.Pixel(-15, -30),
						zIndex:99,
						autoRotation: true,
					});
					this.end=new AMap.Marker({
						map: _this.mapbus,
						position: [_this.startend[1].longitude,_this.startend[1].latitude],
					content:"<img src='"+icon2+"' style='height: 30px;width: 30px' />",
					offset: new AMap.Pixel(-15, -30),
					zIndex:99,
						autoRotation: true,
					});
					for(var i in _this.covers){
						var point=_this.covers[i]
						lineArr.push([point.longitude,point.latitude])
						
					}
					for(var i in stationList){
						var point=stationList[i]
						new AMap.Marker({
							map: _this.mapbus,
							position: [point.longitude,point.latitude],
							offset: new AMap.Pixel(-5, -5),
							content:"<img src='"+icon1+"' style='height: 10px;width: 10px' />",
							zIndex:99,
							autoRotation: true,
						});
					}
					var polyline = new AMap.Polyline({
						map: _this.mapbus,
						path: lineArr,
						showDir: true,
						strokeColor: "#28F", //线颜色
						// strokeOpacity: 1,     //线透明度
						strokeWeight: 3, //线宽
						//strokeStyle: "solid"  //线样式
					});
					
				}, e => {
					//_this.mui.toast('地图加载失败');
					console.log('地图加载失败', e)
				})
			},
		}
	}
</script>

<style>

</style>
