百度地图 vue 基本使用

百度地图参考文档:https://lbsyun.baidu.com/index.php?title=jspopularGL

//统一设置
import marker from '@/assets/marker1.png'

const p1 = new BMap.Point(121.286742, 31.132338)
const p2 = new BMap.Point(121.286742, 31.132338)
const map = new BMap.Map('map')
map.centerAndZoom(p1, 11)

经纬度自定义坐标icon

 // 设置icon
 const startIcon = new BMap.Icon(this.formData.type === 2 ? marker2 : marker5, new BMap.Size(32, 39), {
   anchor: new BMap.Size(20, 39), // icon坐标位置
   imageSize: new BMap.Size(29, 29),//设置图片展示大小
   imageOffset: new BMap.Size(0, 0) // 设置图片偏移
 })
 const startMarker = new BMap.Marker(p1, { icon: marker })
 map.addOverlay(startMarker)

根据经纬度画直线

const driverPolylin = new BMap.Polyline([p1, p2], { strokeColor: '#EF8F2F', strokeWeight: 4, strokeOpacity: 1, strokeStyle: 'dashed' }) // 创建折线
map.addOverlay(driverPolylin)

根据两点规划出开车路线且路线自定义样式

const driving = new BMap.DrivingRoute(map, { renderOptions: { map, autoViewport: true }})
driving.search(p1, p2)
// 重新设置路线颜色
driving.setPolylinesSetCallback((result) => {
  // 清除路线规划
  driving.clearResults()
  const points = []
  result[0].Br.map(function(item) {
    points.push(new BMap.Point(item.lng, item.lat))
  })
  const polyline = new BMap.Polyline(points, { strokeColor: '#558EF7', strokeWeight: 4, strokeOpacity: 1, 'enableDragging': true }) // 创建折线
  map.addOverlay(polyline) // 增加折线
})

如果路线规划重新设置样式 与 两点之间直线同时存在,直线需要放到重新设置路线样式里面,不然清除路线规划会把直线清掉

多个坐标单独移除指定的覆盖物

//获取所有的坐标点,需要移除的坐标单组增加个name 进行判断
const allOverlay = map.getOverlays() 
allOverlay.map(item => {
  if (item.name === 'driverIcon') {
    map.removeOverlay(item)
  }
})

清除地图上所有覆盖物

map.clearOverlays();    

自定义覆盖物(类如图片是正方形,但是标点想展示圆形的可使用)官方demo

// 自定义覆盖物
function customOverlay(point) {
  this._point = point
}
// 继承BMapGL.Overlay
customOverlay.prototype = new BMap.Overlay()
// 实现initialize方法来创建自定义覆盖物
const that = this
customOverlay.prototype.initialize = function(map) {
  this._map = map
  // var pixel = map.pointToOverlayPixel(this._point, {
  //   useRound: false, // 设置此参数,防止抖动
  //   fixPosition: true // 覆盖物跨越180时修正位置
  // })
  const div = (this._div = document.createElement('div'))
  div.style.position = 'absolute'
  div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat)
  // div.style.backgroundColor = 'red'
  // div.style.color = 'white'
  // div.style.padding = '4px 8px'
  // div.style.borderRadius = '50%'
  // div.style.cursor = 'pointer'
  // div.style.whiteSpace = 'nowrap'
  // div.style.MozUserSelect = 'none'
  // div.style.fontSize = '12px'
  // div.style.lineHeight = '1.5'
  // div.innerHTML = this._text

  const img = document.createElement('img')
  img.style.width = '28px'
  img.style.height = '28px'
  img.style.borderRadius = '50%'
  img.src = (that.userLogo || marker1)  //图片路径
  div.appendChild(img)
  map.getPanes().labelPane.appendChild(div)
  return div
}
customOverlay.prototype.draw = function() {
  const map = this._map
  const pixel = map.pointToOverlayPixel(this._point)
  this._div.style.left = pixel.x - 13.5 + 'px' //根据实际位置调试
  this._div.style.top = pixel.y - 10 + 'px' //根据实际位置调试
}
// 创建自定义覆盖物对象
const myCompOverlay = new customOverlay(new BMap.Point(this.userLng, this.userLat))  // 经纬度根据实际使用更换
map.addOverlay(myCompOverlay)

如果需要一秒更新一次位置的话,我这边是用promise先清掉之前的位置再重现展示,不然会显示多个,如果路线规划与直线同时存在,这两个都需要放到循环里面,不然会不显示

每天一个新知识,一起加油吧!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
想要使用百度地图基本功能,首先需要在 Vue 项目中引入百度地图 JavaScript API。可以在 index.html 中直接引入: ```html <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script> ``` 其中,密钥需要在百度地图开放平台申请获得。 接着,在 Vue 组件中可以使用百度地图提供的全局对象 `BMap` 来创建地图实例和添加覆盖物等操作。以下是一个简单的示例: ```html <template> <div id="map"></div> </template> <script> export default { mounted() { // 创建地图实例 const map = new BMap.Map("map") // 设置中心点和缩放级别 const point = new BMap.Point(116.404, 39.915) map.centerAndZoom(point, 15) // 添加标注 const marker = new BMap.Marker(point) map.addOverlay(marker) } } </script> <style> #map { width: 100%; height: 500px; } </style> ``` 在上面的示例中,我们在 `mounted` 钩子函数中创建了地图实例,并在其中设置了地图的中心点和缩放级别。然后使用 `BMap.Marker` 类创建了一个标注,并添加到地图上。 注意,为了让地图显示出来,我们需要在组件的模板中添加一个 `<div>` 元素,并设置其宽度和高度,用于容纳地图。在上面的示例中,我们设置了一个 `id` 为 `map` 的 `<div>` 元素,并在样式中设置了它的宽度为 100%、高度为 500px。 当然,百度地图提供的功能远不止于此,你可以根据自己的需求来使用其他 API,例如搜索、路线规划、地图事件等等。更多详细的使用方法可以参考百度地图 JavaScript API 的官方文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值