Carmap.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <template>
  2. <view>
  3. <view id="container" :style="myStyle"></view>
  4. <u-icon name="map-fill" v-if="false" class="input-card" style="width: 24rem;top: 130px;width: 20px;height: 20px;" color="#999" size="40"></u-icon>
  5. </view>
  6. </template>
  7. <script>
  8. import MapLoader from '@/utils/AMap'
  9. export default {
  10. name:"Carmap",
  11. data() {
  12. return {
  13. myStyle:"width: 100%; height: 140px;",
  14. longitude: 112.276527,
  15. latitude: 30.306427,
  16. AMap:null,
  17. //车的位置
  18. car: {
  19. obj:null,
  20. longitude: '',
  21. latitude: '',
  22. },
  23. down: {
  24. obj:null,
  25. longitude: '',
  26. latitude: '',
  27. },
  28. up: {
  29. obj:null,
  30. longitude: '',
  31. latitude: '',
  32. },
  33. //个人
  34. info: {
  35. obj:null,
  36. init: false,
  37. longitude: '',
  38. latitude: '',
  39. },
  40. mapbus:null,
  41. startend: [{
  42. longitude:112.279274,
  43. latitude:30.303273,
  44. name:''
  45. }, {
  46. longitude:112.273867,
  47. latitude:30.309817,
  48. name:''
  49. }],
  50. start:{},
  51. end:{},
  52. carList:[],
  53. covers: [
  54. {
  55. longitude:112.273867,
  56. latitude:30.309817,
  57. },{
  58. longitude:112.280261,
  59. latitude:30.3041,
  60. },{
  61. longitude:112.279274,
  62. latitude:30.303273,
  63. }
  64. ]
  65. };
  66. },
  67. methods:{
  68. setCarList(sz){
  69. if(this.AMap==null){
  70. return
  71. }
  72. for(var i in this.carList){
  73. this.mapbus.remove(this.carList[i]);
  74. }
  75. var icon0 = require("@/static/img/icon-busPosition.png");
  76. this.carList=[]
  77. for(var i in sz){
  78. var ob=sz[i];
  79. var ob1=new AMap.Marker({
  80. map: this.mapbus,
  81. position: [ob.longitude,ob.latitude],
  82. //offset: new AMap.Pixel(-5, -5),
  83. offset: new AMap.Pixel(-18, -40),
  84. content:"<img src='"+icon0+"' style='height: 40px;width: 40px' />",
  85. zIndex:900,
  86. autoRotation: true,
  87. });
  88. this.myEmit("car",ob1,ob)
  89. this.carList.push(ob1)
  90. }
  91. },
  92. myEmit(type,ob1,obj){
  93. var _this=this;
  94. AMap.event.addListener(ob1, 'click', function(e) {
  95. _this.$emit('clickMap',{
  96. type:type,
  97. obj:obj
  98. })
  99. })
  100. },
  101. setMyStyle(s){
  102. this.myStyle=s;
  103. },
  104. setStartend(sz){
  105. this.start.setPosition( [sz[0].longitude,sz[0].latitude]);
  106. this.end.setPosition( [sz[sz.length-1].longitude,sz[sz.length-1].latitude]);
  107. },
  108. setCenter(item){
  109. if(this.mapbus){
  110. this.mapbus.setCenter([item.longitude,item.latitude]);
  111. return true
  112. }else{
  113. return false
  114. }
  115. },
  116. setSite(ob){
  117. if(this.AMap==null){
  118. return
  119. }
  120. this.down.longitude=ob.longitude
  121. this.down.latitude=ob.latitude
  122. if(!this.down.ob){
  123. var icon0 = require("@/static/img/map_4.png");
  124. this.down.ob= new AMap.Marker({
  125. map: this.mapbus,
  126. position: [this.down.longitude,this.down.latitude],
  127. offset: new AMap.Pixel(-15, -30),
  128. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  129. zIndex:999,
  130. autoRotation: true,
  131. });
  132. }else{
  133. this.down.ob.setPosition( [this.down.longitude,this.down.latitude]);
  134. }
  135. },
  136. setDown(ob){
  137. if(this.AMap==null){
  138. return
  139. }
  140. this.down.longitude=ob.longitude
  141. this.down.latitude=ob.latitude
  142. if(!this.down.ob){
  143. var icon0 = require("@/static/img/down.png");
  144. this.down.ob= new AMap.Marker({
  145. map: this.mapbus,
  146. position: [this.down.longitude,this.down.latitude],
  147. offset: new AMap.Pixel(-15, -30),
  148. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  149. zIndex:999,
  150. autoRotation: true,
  151. });
  152. }else{
  153. this.down.ob.setPosition( [this.down.longitude,this.down.latitude]);
  154. }
  155. },
  156. setUp(ob){
  157. if(this.AMap==null){
  158. return
  159. }
  160. this.up.longitude=ob.longitude
  161. this.up.latitude=ob.latitude
  162. if(!this.up.ob){
  163. var icon0 = require("@/static/img/up.png");
  164. this.up.ob= new AMap.Marker({
  165. map: this.mapbus,
  166. position: [this.up.longitude,this.up.latitude],
  167. offset: new AMap.Pixel(-15, -30),
  168. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  169. zIndex:999,
  170. autoRotation: true,
  171. });
  172. }else{
  173. this.up.ob.setPosition( [this.up.longitude,this.up.latitude]);
  174. }
  175. },
  176. setCar(ob){
  177. if(this.AMap==null){
  178. return
  179. }
  180. this.car.longitude=ob.longitude
  181. this.car.latitude=ob.latitude
  182. if(!this.car.ob){
  183. var icon0 = require("@/static/img/icon-busPosition.png");
  184. this.car.ob= new AMap.Marker({
  185. map: this.mapbus,
  186. position: [this.car.longitude,this.car.latitude],
  187. offset: new AMap.Pixel(-18, -40),
  188. content:"<img src='"+icon0+"' style='height: 40px;width: 40px' />",
  189. zIndex:900,
  190. autoRotation: true,
  191. });
  192. var _this=this;
  193. this.myEmit("car",this.car.ob,ob)
  194. }else{
  195. this.car.ob.setPosition( [this.car.longitude,this.car.latitude]);
  196. }
  197. },
  198. setPerson(ob,bl){
  199. if(this.AMap==null){
  200. return
  201. }
  202. this.info.longitude=ob.longitude
  203. this.info.latitude=ob.latitude
  204. if(!this.info.ob){
  205. var icon0 = require("@/static/img/icon-userPosition.png");
  206. this.info.ob= new AMap.Marker({
  207. map: this.mapbus,
  208. position: [this.info.longitude,this.info.latitude],
  209. //offset: new AMap.Pixel(-5, -5),
  210. offset: new AMap.Pixel(-15, -30),
  211. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  212. zIndex:99,
  213. autoRotation: true,
  214. });
  215. //this.mapbus.setCenter([this.info.longitude,this.info.latitude]); //设置地图中心点
  216. if(!bl){
  217. this.mapbus.setCenter([ this.info.longitude,this.info.latitude]); //设置地图中心点
  218. }
  219. }else{
  220. this.info.ob.setPosition( [this.info.longitude,this.info.latitude]);
  221. }
  222. },
  223. init(){
  224. var _this = this;
  225. MapLoader().then(AMap => {
  226. this.AMap=AMap;
  227. _this.mapbus && _this.mapbus.destroy();
  228. _this.mapbus=new AMap.Map("container", {
  229. resizeEnable: true,
  230. center: [this.longitude, this.latitude],
  231. zoom: 14
  232. });
  233. if(document.getElementsByClassName("amap-logo").length){
  234. document.getElementsByClassName("amap-logo")[0].setAttribute("class","amap-copyright" );
  235. }
  236. this.$emit('onload')
  237. }, e => {
  238. //_this.mui.toast('地图加载失败');
  239. console.log('地图加载失败', e)
  240. })
  241. },
  242. initMap(point,stationList,startend){
  243. var _this = this;
  244. this.covers=point;
  245. this.startend=startend;
  246. MapLoader().then(AMap => {
  247. this.AMap=AMap;
  248. _this.mapbus && _this.mapbus.destroy();
  249. _this.mapbus=new AMap.Map("container", {
  250. resizeEnable: true,
  251. center: [this.longitude, this.latitude],
  252. zoom: 14
  253. });
  254. var lineArr=[]
  255. var icon0 = require("@/static/img/map_0.png");
  256. var icon1 = require("@/static/img/map_1.png");
  257. var icon2 = require("@/static/img/map_2.png");
  258. this.start=new AMap.Marker({
  259. map: _this.mapbus,
  260. position: [_this.startend[0].longitude,_this.startend[0].latitude],
  261. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  262. offset: new AMap.Pixel(-15, -30),
  263. zIndex:99,
  264. autoRotation: true,
  265. });
  266. this.end=new AMap.Marker({
  267. map: _this.mapbus,
  268. position: [_this.startend[1].longitude,_this.startend[1].latitude],
  269. content:"<img src='"+icon2+"' style='height: 30px;width: 30px' />",
  270. offset: new AMap.Pixel(-15, -30),
  271. zIndex:99,
  272. autoRotation: true,
  273. });
  274. for(var i in _this.covers){
  275. var point=_this.covers[i]
  276. lineArr.push([point.longitude,point.latitude])
  277. }
  278. for(var i in stationList){
  279. var point=stationList[i]
  280. var ob_point =new AMap.Marker({
  281. map: _this.mapbus,
  282. position: [point.longitude,point.latitude],
  283. offset: new AMap.Pixel(-5, -7),
  284. content:"<img src='"+icon1+"' style='height: 10px;width: 10px' />",
  285. zIndex:99,
  286. autoRotation: true,
  287. });
  288. this.myEmit("point",ob_point,point)
  289. }
  290. var polyline = new AMap.Polyline({
  291. map: _this.mapbus,
  292. path: lineArr,
  293. showDir: true,
  294. strokeColor: "#28F", //线颜色
  295. // strokeOpacity: 1, //线透明度
  296. strokeWeight: 3, //线宽
  297. //strokeStyle: "solid" //线样式
  298. });
  299. this.$emit('onload')
  300. }, e => {
  301. //_this.mui.toast('地图加载失败');
  302. console.log('地图加载失败', e)
  303. })
  304. },
  305. }
  306. }
  307. </script>
  308. <style scoped>
  309. /*去除下标*/
  310. .amap-logo{
  311. display: none !important;
  312. }
  313. .amap-copyright{
  314. display: none !important;
  315. opacity:0;
  316. font-size: 1px;
  317. }
  318. .input-card {
  319. display: flex;
  320. flex-direction: column;
  321. min-width: 0;
  322. word-wrap: break-word;
  323. background-color: #fff;
  324. background-clip: border-box;
  325. border-radius: .25rem;
  326. width: 22rem;
  327. border-width: 0;
  328. border-radius: 0.4rem;
  329. box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
  330. position: fixed;
  331. bottom: 1rem;
  332. right: 1rem;
  333. -ms-flex: 1 1 auto;
  334. flex: 1 1 auto;
  335. padding: 0.75rem 0.75rem;
  336. }
  337. </style>