Carmap.vue 10 KB

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