Carmap.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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(-15, -30),
  84. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  85. zIndex:99,
  86. autoRotation: true,
  87. });
  88. this.carList.push(ob1)
  89. }
  90. },
  91. setMyStyle(s){
  92. this.myStyle=s;
  93. },
  94. setStartend(sz){
  95. this.start.setPosition( [sz[0].longitude,sz[0].latitude]);
  96. this.end.setPosition( [sz[sz.length-1].longitude,sz[sz.length-1].latitude]);
  97. },
  98. setCenter(item){
  99. if(this.mapbus){
  100. this.mapbus.setCenter([item.longitude,item.latitude]);
  101. }
  102. },
  103. setSite(ob){
  104. if(this.AMap==null){
  105. return
  106. }
  107. this.down.longitude=ob.longitude
  108. this.down.latitude=ob.latitude
  109. if(!this.down.ob){
  110. var icon0 = require("@/static/img/map_4.png");
  111. this.down.ob= new AMap.Marker({
  112. map: this.mapbus,
  113. position: [this.down.longitude,this.down.latitude],
  114. offset: new AMap.Pixel(-15, -30),
  115. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  116. zIndex:999,
  117. autoRotation: true,
  118. });
  119. }else{
  120. this.down.ob.setPosition( [this.down.longitude,this.down.latitude]);
  121. }
  122. },
  123. setDown(ob){
  124. if(this.AMap==null){
  125. return
  126. }
  127. this.down.longitude=ob.longitude
  128. this.down.latitude=ob.latitude
  129. if(!this.down.ob){
  130. var icon0 = require("@/static/img/down.png");
  131. this.down.ob= new AMap.Marker({
  132. map: this.mapbus,
  133. position: [this.down.longitude,this.down.latitude],
  134. offset: new AMap.Pixel(-15, -30),
  135. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  136. zIndex:999,
  137. autoRotation: true,
  138. });
  139. }else{
  140. this.down.ob.setPosition( [this.down.longitude,this.down.latitude]);
  141. }
  142. },
  143. setUp(ob){
  144. if(this.AMap==null){
  145. return
  146. }
  147. this.up.longitude=ob.longitude
  148. this.up.latitude=ob.latitude
  149. if(!this.up.ob){
  150. var icon0 = require("@/static/img/up.png");
  151. this.up.ob= new AMap.Marker({
  152. map: this.mapbus,
  153. position: [this.up.longitude,this.up.latitude],
  154. offset: new AMap.Pixel(-15, -30),
  155. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  156. zIndex:999,
  157. autoRotation: true,
  158. });
  159. }else{
  160. this.up.ob.setPosition( [this.up.longitude,this.up.latitude]);
  161. }
  162. },
  163. setCar(ob){
  164. if(this.AMap==null){
  165. return
  166. }
  167. this.car.longitude=ob.longitude
  168. this.car.latitude=ob.latitude
  169. if(!this.car.ob){
  170. var icon0 = require("@/static/img/icon-busPosition.png");
  171. this.car.ob= new AMap.Marker({
  172. map: this.mapbus,
  173. position: [this.car.longitude,this.car.latitude],
  174. offset: new AMap.Pixel(-5, -5),
  175. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  176. zIndex:99,
  177. autoRotation: true,
  178. });
  179. }else{
  180. this.car.ob.setPosition( [this.car.longitude,this.car.latitude]);
  181. }
  182. },
  183. setPerson(ob){
  184. if(this.AMap==null){
  185. return
  186. }
  187. this.info.longitude=ob.longitude
  188. this.info.latitude=ob.latitude
  189. if(!this.info.ob){
  190. var icon0 = require("@/static/img/icon-userPosition.png");
  191. this.info.ob= new AMap.Marker({
  192. map: this.mapbus,
  193. position: [this.info.longitude,this.info.latitude],
  194. //offset: new AMap.Pixel(-5, -5),
  195. offset: new AMap.Pixel(-15, -30),
  196. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  197. zIndex:99,
  198. autoRotation: true,
  199. });
  200. //this.mapbus.setCenter([this.info.longitude,this.info.latitude]); //设置地图中心点
  201. this.mapbus.setCenter([ this.info.longitude,this.info.latitude]); //设置地图中心点
  202. }else{
  203. this.info.ob.setPosition( [this.info.longitude,this.info.latitude]);
  204. }
  205. },
  206. init(){
  207. var _this = this;
  208. MapLoader().then(AMap => {
  209. this.AMap=AMap;
  210. _this.mapbus && _this.mapbus.destroy();
  211. _this.mapbus=new AMap.Map("container", {
  212. resizeEnable: true,
  213. center: [this.longitude, this.latitude],
  214. zoom: 14
  215. });
  216. this.$emit('onload')
  217. }, e => {
  218. //_this.mui.toast('地图加载失败');
  219. console.log('地图加载失败', e)
  220. })
  221. },
  222. initMap(point,stationList,startend){
  223. var _this = this;
  224. this.covers=point;
  225. this.startend=startend;
  226. MapLoader().then(AMap => {
  227. this.AMap=AMap;
  228. _this.mapbus && _this.mapbus.destroy();
  229. _this.mapbus=new AMap.Map("container", {
  230. resizeEnable: true,
  231. center: [this.longitude, this.latitude],
  232. zoom: 14
  233. });
  234. var lineArr=[]
  235. var icon0 = require("@/static/img/map_0.png");
  236. var icon1 = require("@/static/img/map_1.png");
  237. var icon2 = require("@/static/img/map_2.png");
  238. this.start=new AMap.Marker({
  239. map: _this.mapbus,
  240. position: [_this.startend[0].longitude,_this.startend[0].latitude],
  241. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  242. offset: new AMap.Pixel(-15, -30),
  243. zIndex:99,
  244. autoRotation: true,
  245. });
  246. this.end=new AMap.Marker({
  247. map: _this.mapbus,
  248. position: [_this.startend[1].longitude,_this.startend[1].latitude],
  249. content:"<img src='"+icon2+"' style='height: 30px;width: 30px' />",
  250. offset: new AMap.Pixel(-15, -30),
  251. zIndex:99,
  252. autoRotation: true,
  253. });
  254. for(var i in _this.covers){
  255. var point=_this.covers[i]
  256. lineArr.push([point.longitude,point.latitude])
  257. }
  258. for(var i in stationList){
  259. var point=stationList[i]
  260. new AMap.Marker({
  261. map: _this.mapbus,
  262. position: [point.longitude,point.latitude],
  263. offset: new AMap.Pixel(-5, -5),
  264. content:"<img src='"+icon1+"' style='height: 10px;width: 10px' />",
  265. zIndex:99,
  266. autoRotation: true,
  267. });
  268. }
  269. var polyline = new AMap.Polyline({
  270. map: _this.mapbus,
  271. path: lineArr,
  272. showDir: true,
  273. strokeColor: "#28F", //线颜色
  274. // strokeOpacity: 1, //线透明度
  275. strokeWeight: 3, //线宽
  276. //strokeStyle: "solid" //线样式
  277. });
  278. this.$emit('onload')
  279. }, e => {
  280. //_this.mui.toast('地图加载失败');
  281. console.log('地图加载失败', e)
  282. })
  283. },
  284. }
  285. }
  286. </script>
  287. <style scoped>
  288. /*去除下标*/
  289. .amap-logo{
  290. display: none;
  291. }
  292. .amap-copyright{
  293. opacity:0;
  294. font-size: 1px;
  295. }
  296. .input-card {
  297. display: flex;
  298. flex-direction: column;
  299. min-width: 0;
  300. word-wrap: break-word;
  301. background-color: #fff;
  302. background-clip: border-box;
  303. border-radius: .25rem;
  304. width: 22rem;
  305. border-width: 0;
  306. border-radius: 0.4rem;
  307. box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
  308. position: fixed;
  309. bottom: 1rem;
  310. right: 1rem;
  311. -ms-flex: 1 1 auto;
  312. flex: 1 1 auto;
  313. padding: 0.75rem 0.75rem;
  314. }
  315. </style>