前端 百度地图绘制路线加上图片

使用百度官方示例的方法根据起终点经纬度查询驾车路线但是只是一个线路

<template>
  <div class="transportInfo">
    <div id="mapcontainer" class="map">
      11
    </div>
    <div class="collapse">
      <el-collapse v-model="activeNames" @change="handleChange">
        <el-collapse-item title="运输详情" name="1">
          <div class="transportHead">
            <div class="img"><img src="@/assets/images/sj.png" /></div>
            <div>
              <div>
                <span class="label">运输人员:</span
                ><span class="value">李荣/12345678901</span>
              </div>
              <div>
                <span class="label1">浙A·DA4563</span
                ><span class="value1">配送中</span>
              </div>
            </div>
          </div>
          <div class="timeline">
            <el-timeline>
              <el-timeline-item
                v-for="(activity, index) in activities"
                :key="index"
                :timestamp="activity.timestamp"
                :color="activity.color"
                placement="top"
              >
                <span style="color:rgba(12, 111, 255, 1)">{{
                  activity.person
                }}</span>
                <span style="color:rgba(26, 26, 26, 1)">{{
                  activity.renwu
                }}</span>
              </el-timeline-item>
            </el-timeline>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>
<script>
import working from '@/assets/images/working.png'
import circle from '@/assets/images/circle.png'
export default {
  data() {
    return {
      activeNames: ['1'],
      activities: [
        {
          person: '主管部门',
          renwu: '创建送货单',
          timestamp: '2023年3月3日   18:00',
          color: 'rgba(0, 180, 42, 1)',
          size: 'normal '
        },
        {
          person: '运输人员 李荣',
          renwu: '开始运输',
          timestamp: '2018-04-13'
        },
        {
          person: '运输人员 李荣',
          renwu: '进行核验',
          timestamp: '2018-04-13'
        },
        {
          person: '工地人员 王狄锋',
          renwu: '确认签收',
          timestamp: '2018-04-11'
        }
      ]
    }
  },
  created() {},
  mounted() {
    // 百度地图API功能
    // var map = new BMap.Map('allmap')
    // map.centerAndZoom(new BMap.Point(116.404, 39.915), 11)
    // var p1 = new BMap.Point(116.301934, 39.977552)
    // var p2 = new BMap.Point(116.508328, 39.919141)
    // var driving = new BMap.DrivingRoute(map, {
    //   renderOptions: {
    //     map: map,
    //     autoViewport: true
    //   }
    // })
    // driving.search(p1, p2)
    setTimeout(() => {
      $(document).ready(function() {
        // var car1 = [
        //   ['113.208619', '23.170208', '广州', '装车', '2016-12-05 19:47:03'],
        //   ['112.622218', '26.979794', '', '装车', '2016-12-05 19:47:03'],
        //   [
        //     '113.006332',
        //     '28.263503',
        //     '长沙',
        //     '当前位置',
        //     '2016-12-05 19:47:03'
        //   ],
        //   ['111.731111', '40.842', , '呼和浩特', '暂未到达目的地']
        // ]
        var car2 = [
          ['120.230199', '30.215376', '', '杭州', '起点'],
          ['108.945456', '34.366566', '西安', '当前位置', '4km'],
          ['87.504831', '43.937895', , '乌鲁木齐', '终点']
        ]
        var mp = new BMap.Map('mapcontainer', { enableMapClick: false })
        mp.centerAndZoom(new BMap.Point(112.438233, 34.654336), 6)
        mp.enableScrollWheelZoom()
        // currentLocation(['113.006332', '28.263503'], car1)
        currentLocation(['108.945456', '34.366566'], car2)
        //标注当前车辆坐标位置
        function currentLocation(curPosArr, carArr) {
          var curPt = new BMap.Point(curPosArr[0], curPosArr[1]) //当前位置
          var curIcon = new BMap.Icon(working, new BMap.Size(52, 52))
          var curMarker = new BMap.Marker(curPt, { icon: curIcon })
          mp.addOverlay(curMarker)
          var isDraw = false //是否已经绘制过路线
          curMarker.onclick = function() {
            drawPath(carArr, isDraw)
            isDraw = true
          }
        }
        //绘制路线
        function drawPath(carArr, isDraw) {
          if (isDraw) {
            //若绘制过路线  返回 false
            return false
          }
          var pointArr = []
          var ptNum = 0
          var driving = new BMap.DrivingRoute(mp) //创建驾车实例
          // 复杂的自定义覆盖物
          function ComplexCustomOverlay(point, state, time) {
            this._point = point
            this.state = state
            this.time = time
          }
          ComplexCustomOverlay.prototype = new BMap.Overlay()
          ComplexCustomOverlay.prototype.initialize = function(map) {
            this._map = map
            var div = (this._div = document.createElement('div'))
            $(div).addClass('state-wrap')
            var str = '<div class="logistics-wrap">'
            str += `<div class="logistics-time">` + this.time + '</div>'
            str += '<div class="logistics-state">' + this.state + '</div>'
            str += '</div>'
            div.innerHTML = str
            mp.getPanes().labelPane.appendChild(div)
            var he = div.offsetHeight
            this._he = he //当前div的高度
            return div
          }
          ComplexCustomOverlay.prototype.draw = function() {
            var map = this._map
            var pixel = map.pointToOverlayPixel(this._point)
            this._div.style.left = pixel.x - 24 + 'px'
            this._div.style.top = pixel.y - this._he + 5 + 'px'
          }
          /*自定义复杂覆盖物结束*/
          for (var i = 0, len = carArr.length; i < len; i++) {
            var point = new BMap.Point(carArr[i][0], carArr[i][1])
            pointArr[i] = point
            var myIcon = new BMap.Icon(circle, new BMap.Size(16, 16))
            var marker = new BMap.Marker(point, { icon: myIcon }) // 创建标注
            mp.addOverlay(marker) // 将标注添加到地图中
            //此处解决在for循环中添加事件总是执行最后一个的情况,传入参数并且立即执行
            ;(function(point, state, time) {
              var myComOverlay = new ComplexCustomOverlay(point, state, time)
              mp.addOverlay(myComOverlay)
              marker.onclick = function() {
                //给各个点添加点击事件,显示、隐藏自定义复杂物
                if (myComOverlay.isVisible()) {
                  myComOverlay.hide()
                } else {
                  myComOverlay.show()
                }
              }
            })(point, carArr[i][3], carArr[i][4])
          }
          var len = pointArr.length - 1
          initRoute(ptNum)
          function initRoute(num) {
            driving.search(pointArr[num], pointArr[num + 1])
            driving.setSearchCompleteCallback(function() {
              var plan = driving.getResults().getPlan(0)
              var pts = plan.getRoute(0).getPath()
              var lineCor = ptNum == len - 1 ? 'red' : '#1aea0a'
              var lineSty = ptNum == len - 1 ? 'dashed' : 'solid'
              var polyline = new BMap.Polyline(pts, {
                strokeColor: lineCor,
                strokeWeight: 3,
                strokeOpacity: 0.8,
                strokeStyle: lineSty
              })
              mp.addOverlay(polyline)
              //查找下两个点
              ptNum++
              if (ptNum < len) {
                initRoute(ptNum)
              }
            })
          }
          mp.setViewport(pointArr) //自动调整视野
        }
      })
    })
  },
  methods: {
    handleChange(val) {
      console.log(val)
    }
  }
}
</script>
<style lang="less" scoped>
.transportInfo {
  padding: 0px 16px 16px 16px;
  // background-color: #ccc;
  position: relative;
  .map {
    width: 100%;
    height: calc(100vh - 220px);
  }
  .collapse {
    .el-collapse {
      position: absolute;
      top: 32px;
      left: 32px;
      width: 314px;
      border-radius: 2px;
      background: rgba(255, 255, 255, 1);

      /deep/.el-collapse-item__content {
        padding-bottom: 0px !important;
      }
      /deep/.el-collapse-item__header::before {
        content: '';
        width: 4px;
        height: 16px;
        background: rgba(12, 111, 255, 1);
        margin-right: 8px;
        margin-left: 14px;
      }
      /deep/.el-collapse-item__header {
        font-size: 14px;
        font-weight: 700;
        color: rgba(26, 26, 26, 1);
        .el-collapse-item__arrow.el-icon-arrow-right {
          color: rgba(25.5, 25.5, 25.5, 1);
        }
      }
      .transportHead {
        display: flex;
        margin-left: 14px;
        .img {
          margin-right: 8px;
        }
        .label {
          font-size: 14px;
          font-weight: 400;
          line-height: 22px;
          color: rgba(26, 26, 26, 1);
        }
        .value {
          font-size: 14px;
          font-weight: 400;
          line-height: 22px;
          color: rgba(102, 102, 102, 1);
        }
        .label1 {
          border-radius: 2px;
          background: rgba(242, 247, 255, 1);
          border: 1px solid rgba(217, 217, 217, 1);
          font-size: 12px;
          font-weight: 700;
          line-height: 22px;
          color: rgba(26, 26, 26, 1);
          padding: 2px 4px;
          margin-right: 6px;
        }
        .value1 {
          border-radius: 2px;
          background: rgba(255, 141, 26, 0.1);
          padding: 4px 8px 4px 8px;
          font-size: 14px;
          font-weight: 400;
          color: rgba(255, 141, 26, 1);
        }
      }
      .timeline {
        margin: 16px;
        padding: 16px 0 0 0px;
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 2px;
        width: 282px;
        background: rgba(242, 247, 255, 1);
      }
    }
  }
}
</style>
<style>
#mapcontainer {
  width: 100%;
  height: calc(100vh - 220px);
  position: relative;
}
.state-wrap {
  padding-bottom: 16px;
  position: absolute;
  left: 0;
  top: 0;
  font-size: 12px;
  line-height: 16px;
  /* background: url(img/map_label.png) no-repeat left bottom; */
  /* background: red; */
}
.logistics-wrap {
  /* background: #fff; */
  /* padding: 10px; */
  position: absolute;
  right: 0px;
  display: flex;
}
.logistics-state {
  padding-left: 25px;
  height: 25px;
  line-height: 25px;
  width: 65px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
}
.logistics-time {
  width: 30px;
  padding: 4px 8px;
  padding-left: 25px;
  border-radius: 2px 0px, 0px, 2px;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}
/* .logistics-state,
.logistics-time {
  padding-left: 25px;
  height: 25px;
  line-height: 25px;
  width: 125px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
} */
.logistics-state {
  /* background: url(img/state_icon.png) no-repeat left center; */
  background: #fff;
}
.logistics-time {
  /* background: url(img/clock_icon.png) no-repeat left center; */
  background: rgba(0, 122, 255, 1);
  box-shadow: 0px 2px 4px 0px rgba(42, 130, 228, 0.15);
  color: rgba(255, 255, 255, 1);
}
</style>

效果如下:
在这里插入图片描述
参考文章:
https://www.jianshu.com/p/10a5581a8db1?winzoom=1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android应用程序中使用百度地图API绘制实时路线,您需要执行以下步骤: 1. 在百度地图开放平台上注册开发者帐户,创建应用程序并获取API密钥。 2. 在应用程序的build.gradle文件中添加以下依赖项: ``` dependencies { implementation 'com.baidu.android:mapapi:5.0.0' } ``` 3. 在布局文件中添加MapView: ``` <com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 4. 在Activity中初始化MapView和BaiduMap: ``` public class MainActivity extends AppCompatActivity { private MapView mMapView; private BaiduMap mBaiduMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mMapView = findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); } } ``` 5. 使用LocationManager获取设备的实时位置,并在地图上绘制路线: ``` public class MainActivity extends AppCompatActivity { private MapView mMapView; private BaiduMap mBaiduMap; private LocationManager mLocationManager; private Polyline mPolyline; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mMapView = findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); } @Override protected void onResume() { super.onResume(); if (mLocationManager != null) { mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, mLocationListener); } } @Override protected void onPause() { super.onPause(); if (mLocationManager != null) { mLocationManager.removeUpdates(mLocationListener); } } private final LocationListener mLocationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); if (mPolyline == null) { List<LatLng> points = new ArrayList<>(); points.add(latLng); PolylineOptions polylineOptions = new PolylineOptions().points(points).color(Color.BLUE).width(5); mPolyline = (Polyline) mBaiduMap.addOverlay(polylineOptions); } else { List<LatLng> points = mPolyline.getPoints(); points.add(latLng); mPolyline.setPoints(points); } MapStatus mapStatus = new MapStatus.Builder().target(latLng).zoom(18).build(); mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(mapStatus)); } }; } ``` 这样,您的应用程序就可以在地图上绘制实时路线了。请注意,此示例仅使用GPS提供程序跟踪位置。如果您需要更好的精度和可靠性,请考虑使用其他位置提供程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值