Vue引入百度地图增加导航功能

        这两天在开发项目的过程中遇到一个需求需要引入百度地图增加导航功能,话不多说直接上代码

 首先引入百度地图API输入在百度地图API申请的秘钥

<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=申请的key"></script>
<script type="text/javascript" src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=申请的key"></script>
<link rel="stylesheet" href="//api.map.baidu.com/library/SearchInfoWindow/1.5/src/SearchInfoWindow_min.css" />
<script src="//mapopen.bj.bcebos.com/github/BMapGLLib/DrawingManager/src/DrawingManager.min.js"></script>

随后我们先初始化地图

<div id="wapmap" class="amap-wrapper"></div>
mounted() {
        this.BaiDuMapInit();
    },
    methods:{
         // 初始化地图
        BaiDuMapInit() {
            let wapmap = new BMap.Map("wapmap");
            wapmap.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
            wapmap.clearOverlays(); // 清除覆盖物
            let point = new BMap.Point(116.404269,39.916042);
            wapmap.centerAndZoom(point, 15);
        }
    }

创建检索信息窗口对象

 var content = '<div style="margin:0;line-height:20px;padding:2px;">' +
                    '<img src="../img/baidu.jpg" alt="" style="float:right;zoom:1;overflow:hidden;width:100px;height:100px;margin-left:3px;"/>' +
                    '地址:北京市海淀区上地十街10号<br/>电话:(010)59928888<br/>简介:百度大厦位于北京市海淀区西二旗地铁站附近,为百度公司综合研发及办公总部。' +
                  '</div>';
    //创建检索信息窗口对象
    var searchInfoWindow = null;
	searchInfoWindow = new BMapLib.SearchInfoWindow(wapmap , content, {
			title  : "月亮大厦",      //标题
			width  : 290,             //宽度
			height : 105,              //高度
			panel  : "panel",         //检索结果面板
			enableAutoPan : true,     //自动平移
			searchTypes   :[
				BMAPLIB_TAB_SEARCH,   //周边检索
				BMAPLIB_TAB_TO_HERE,  //到这里去
				BMAPLIB_TAB_FROM_HERE //从这里出发
			]
		});

添加marker对象

 var marker = new BMap.Marker(poi); //创建marker对象
    marker.enableDragging(); //marker可拖拽
    marker.addEventListener("click", function(e){
	    searchInfoWindow.open(marker);
    })
    wapmap.addOverlay(marker); //在地图中添加marker

最终页面展示效果

 完整代码

<template>
    <div class="Map">
        <div id="wapmap" class="amap-wrapper"></div>
    </div>
</template>

<script>
export default {
    mounted() {
        this.BaiDuMapInit();
    },
    methods: {
        // 初始化地图
        BaiDuMapInit() {
            let wapmap = new BMap.Map("wapmap");
            wapmap.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
            wapmap.clearOverlays(); // 清除覆盖物
            let point = new BMap.Point(116.404269, 39.916042);
            wapmap.centerAndZoom(point, 15);
            var content =
                '<div style="margin:0;line-height:20px;padding:2px;">' +
                '<img src="../img/baidu.jpg" alt="" style="float:right;zoom:1;overflow:hidden;width:100px;height:100px;margin-left:3px;"/>' +
                "地址:北京市海淀区上地十街10号<br/>电话:(010)59928888<br/>简介:月亮大厦位于北京市海淀区西二旗地铁站附近,为综合研发及办公总部。" +
                "</div>";
            //创建检索信息窗口对象
            var searchInfoWindow = null;
            searchInfoWindow = new BMapLib.SearchInfoWindow(wapmap, content, {
                title: "月亮大厦", //标题
                width: 290, //宽度
                height: 105, //高度
                panel: "panel", //检索结果面板
                enableAutoPan: true, //自动平移
                searchTypes: [
                    BMAPLIB_TAB_SEARCH, //周边检索
                    BMAPLIB_TAB_TO_HERE, //到这里去
                    BMAPLIB_TAB_FROM_HERE, //从这里出发
                ],
            });
            var marker = new BMap.Marker(point); //创建marker对象
            marker.enableDragging(); //marker可拖拽
            marker.addEventListener("click", function (e) {
                searchInfoWindow.open(marker);
            });
            wapmap.addOverlay(marker); //在地图中添加marker
        },
    },
};
</script>

<style lang="scss" scoped>
.Map {
    position: fixed;
    width: 100%;
    height: 100%;
    .amap-wrapper {
        width: 100%;
        height: 100%;
    }
}
</style>

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在Vue中使用高德地图实现导航,首先需要引入高德地图的JavaScript API。可以通过在Vue项目中的index.html文件中添加以下script标签来引入API: ``` <script src="https://webapi.amap.com/maps?v=1.4.15&key=your_api_key"></script> ``` 其中,"your_api_key"需要替换为你在高德地图开放平台申请的API密钥。 接下来,在Vue组件中,可以使用AMap对象来创建地图实例以及实现导航功能。 首先,在vue组件的mounted钩子函数中,实例化地图对象: ```javascript mounted() { const map = new AMap.Map("mapContainer", { zoom: 15, center: [经度, 纬度] }); } ``` 这里,“mapContainer”是一个div元素的id,用来承载地图。 然后,可以使用AMap对象的Driving类来实现导航功能: ```javascript mounted() { const map = new AMap.Map("mapContainer", { zoom: 15, center: [经度, 纬度] }); const driving = new AMap.Driving({ map: map, panel: "naviPanel" }); const startPoint = new AMap.LngLat(起点经度, 起点纬度); const endPoint = new AMap.LngLat(终点经度, 终点纬度); driving.search(startPoint, endPoint, function(status, result) { if (status === 'complete') { console.log('导航搜索成功!'); } else { console.log('导航搜索失败!'); } }); } ``` 在上述代码中,通过实例化Driving类并传入地图对象map导航结果显示的panel容器的id,然后可以使用search方法进行导航搜索。 需要注意的是,经度和纬度需要根据实际情况进行替换。 最后,在Vue组件的template中,添加地图容器和导航结果容器的代码: ```html <template> <div> <div id="mapContainer"></div> <div id="naviPanel"></div> </div> </template> ``` 通过上述步骤,就可以在Vue中调用高德地图实现导航功能了。当然,还可以根据具体需求,调整地图的样式和导航的方式等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值