高德地图 自定义图标加路线规划

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <title>图标点标记</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        html,body,#container{
            height:100%;
            width:100%;
        }
    </style>
</head>
<body>
    <div id="container"></div>
    <script src="https://webapi.amap.com/maps?v=1.4.15&key=您的key&plugin=AMap.Driving"></script>

    <script>
    var map = new AMap.Map('container', {
        zoom: 12,
        center: [116.4,39.92],
        resizeEnable: true
    });

    var startIcon = new AMap.Icon({
        // 图标尺寸
        size: new AMap.Size(25, 25),
        // 图标的取图地址
        image: 'tu.png',
        // 图标所用图片大小
        imageSize: new AMap.Size(25, 25),
    });
    /*
    * 驾车策略 
    * AMap.DrivingPolicy.LEAST_TIME           最快捷模式
    * AMap.DrivingPolicy.LEAST_FEE            最经济模式
    * AMap.DrivingPolicy.LEAST_DISTANCE       最短距离模式
    * AMap.DrivingPolicy.REAL_TRAFFIC         考虑实时路况
    */
    var drivingOption = {
        policy: AMap.DrivingPolicy.LEAST_TIME, // 其它policy参数请参考 https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingPolicy
        ferry: 1, // 是否可以使用轮渡
        map: map,
        hideMarkers: false, // 设置隐藏路径规划的起始点图标
        autoFitView: true
    }

    // 构造路线导航类
    var driving = new AMap.Driving(drivingOption)
    // 根据起终点经纬度规划驾车导航路线
    driving.search(new AMap.LngLat(116.421002, 39.93123), new AMap.LngLat(116.402123, 39.91122), function(status, result) {
      if (status === 'complete') {
        console.log('绘制驾车路线完成')
      } else {
        console.log('获取驾车数据失败:' + result)
      }
    });

    var capitals = [{
        center: [116.42,39.93123],
    }, {
        center: [116.41,39.92132],
    }, {
        center: [116.40,39.91122],
    }];

    var facilities = [];
    for (var i = 0; i < capitals.length; i++) {
        var marker = new AMap.Marker({
            position: new AMap.LngLat(capitals[i].center[0], capitals[i].center[1]),
            offset: new AMap.Pixel(-10, -10),
            icon: startIcon,
        });
        facilities.push(marker);
    }
    map.add(facilities);
    </script>
</body>
</html>

效果图:
在这里插入图片描述

以防自己以后能用到 先写出来。

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue高德地图自定义标记可以通过以下步骤进行: 1. 引入高德地图JavaScript API 在Vue组件的script标签中引入高德地图JavaScript API,例如: ``` <script src="https://webapi.amap.com/maps?v=1.4.15&key=你的key"></script> ``` 其中key为你的高德地图开发者账号的应用key。 2. 定义地图容器 在Vue组件的template标签中定义地图容器,例如: ``` <template> <div id="map-container"></div> </template> ``` 其中id为map-container的div元素用于承载地图。 3. 初始化地图对象 在Vue组件的script标签中,通过AMap.Map类初始化地图对象,例如: ``` <script> export default { mounted() { const map = new AMap.Map('map-container', { zoom: 13, center: [116.397428, 39.90923] }); } } </script> ``` 其中zoom表示地图缩放级别,center表示地图中心点坐标。 4. 自定义标记图标 在Vue组件的script标签中,通过AMap.Icon类定义自定义标记图标,例如: ``` <script> export default { mounted() { const icon = new AMap.Icon({ size: new AMap.Size(50, 50), image: 'https://webapi.amap.com/theme/v1.3/images/newpc/way_btn2.png', imageSize: new AMap.Size(50, 50) }); } } </script> ``` 其中size表示标记图标尺寸,image表示标记图标图片地址,imageSize表示标记图标显示尺寸。 5. 添自定义标记 在Vue组件的script标签中,通过AMap.Marker类添自定义标记,例如: ``` <script> export default { mounted() { const map = new AMap.Map('map-container', { zoom: 13, center: [116.397428, 39.90923] }); const icon = new AMap.Icon({ size: new AMap.Size(50, 50), image: 'https://webapi.amap.com/theme/v1.3/images/newpc/way_btn2.png', imageSize: new AMap.Size(50, 50) }); const marker = new AMap.Marker({ position: [116.397428, 39.90923], icon: icon, offset: new AMap.Pixel(-25, -25) }); marker.setMap(map); } } </script> ``` 其中position表示标记位置坐标,icon表示标记图标,offset表示标记图标偏移量。 6. 完整代码 ``` <template> <div id="map-container"></div> </template> <script> export default { mounted() { const map = new AMap.Map('map-container', { zoom: 13, center: [116.397428, 39.90923] }); const icon = new AMap.Icon({ size: new AMap.Size(50, 50), image: 'https://webapi.amap.com/theme/v1.3/images/newpc/way_btn2.png', imageSize: new AMap.Size(50, 50) }); const marker = new AMap.Marker({ position: [116.397428, 39.90923], icon: icon, offset: new AMap.Pixel(-25, -25) }); marker.setMap(map); } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值