在此分享一些我对高德地图的使用,区域绘制(颜色区分),地图下段,点击交互,路线绘制,立体公告牌,以及兴趣点位绘制,兴趣点鼠标移入后显示弹窗(内附视频)

地图交互展示

在此我分享一下重点代码以及交互,有不足支出请多多指教

在此我仅仅展示色块地图下段,全路线地图下段暂不做展示了

前面的代码就是各种附着物的绘制,将他们互相之间的业务串联起来的点击事件放在最后

第一步在main.js里放入

window._AMapSecurityConfig = {
  securityJsCode: "自己申请的Code",
};

创建业务页面

  <div class="map-container" id="container"></div>
   <!-- 创建容器 -->

定好要添加哪些内容用后期控制显隐清理旧的附着物绘制想要的

let layer, ScutcheonNumberLocal, ScutcheonNumber, zMarker, infoWindow
//  layer: 地图层 绘制的路线
//  ScutcheonNumberLocal: 数字牌基础层
//  ScutcheonNumber: 数字牌层
//  zMarker: 文字主体层
//  infoWindow :路线详情弹窗实例
   //此方法统一写不同情况下的地图附着物清理
        mapRemove() {
            //清除数字路牌
            ScutcheonNumberLocal.remove(zMarker)
            //清除兴趣点
            if (this.markerList.length > 0) {
                this.markerList.forEach(e => {
                    this.map.remove(e);
                })
                //清理结束,路线实例储存置空
                this.markerList = []
            }
            //清除路线
            if (this.layerList.length > 0) {
                this.layerList.forEach(e => {
                    this.map.removeOverlay(e)
                })
                //清理结束,路线实例储存置空
                this.layerList = []
            }
        },

初始化地图

 //初始化地图只执行一次
        initMap() {
            let that = this;
            AMapLoader.load({
                key: '自己的key',
                version: '2.0',
                plugins: ['AMap.Scale', 'AMap.ToolBar'],
                AMapUI: {
                    version: '1.1',
                    plugins: [
                        'geo/DistrictExplorer',//行政区域搜索
                        'overlay/SimpleMarker', //SimpleMarker
                        'overlay/SimpleInfoWindow',//SimpleInfoWindow
                        'control/BasicControl'
                    ]
                },
                Loca: {
                    version: '2.0.0',
                },
            })
                .then((AMap) => {
                    that.getDis(AMap);
                })
                .catch((e) => { });
        },

初始化地图中心点,图层,风格样式等配置项

 //初始化地图部分 只执行一次
        getDis(AMap) {
            var that = this
            that.map = new AMap.Map('container', {
                zoom: 8.7,
                center: [112.33479, 32.98615],
                // resizeEnable: true,
                showLabel: true,
                viewMode: '3D',
                pitch: 40,
                rotation: 3,
                // skyColor: 'rgba(0, 0,  0,0.58)',
                // mapStyle: 'amap://styles/grey',
                mapStyle: "用自己的具体参考官网注释掉用原始风格也没影响",
                layers: [
                    //只显示默认图层的时候,layers可以缺省
                    AMap.createDefaultLayer() //高德默认标准图层
                ]
            });
            AMapUI.loadUI(['control/BasicControl'], function (BasicControl) {
                //缩放控件
                that.map.addControl(new BasicControl.Zoom({
                    position: {
                        top: '100px',
                        right: '500px',
                    }, //left top,左上角
                    showZoomNum: true //显示zoom值
                }));
            });
            this.addArea()
        },

添3D路牌,以及行政区色块

  //初始化数字牌
        initMapScutcheon() {
//featuresList 是从自己接口拿的路牌数据
    this.featuresList =     [
    {
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": [
                111.849392,
                33.044865
            ]
        },
        "properties": {
            "key": "内乡县",
            "value": 7
        }
    },
    
    {
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": [
                112.948245,
                33.056109
            ]
        },
        "properties": {
            "key": "社旗县",
            "value": 0
        }
    }
]
            var scutcheonNumberData = {
                "type": "FeatureCollection",
                "features": this.featuresList
            }
            this.drawScutcheonNumber(scutcheonNumberData)
        },
        //绘制数字牌
        drawScutcheonNumber(scutcheonNumberData) {
            var that = this
            ScutcheonNumberLocal = new Loca.Container({
                map: that.map,
            });
            ScutcheonNumber = new Loca.GeoJSONSource({
                data: scutcheonNumberData
            });
            // 文字主体图层
            zMarker = new Loca.ZMarkerLayer({
                zIndex: 120,
                depth: false,
            });
            zMarker.setSource(ScutcheonNumber);
            zMarker.setStyle({
                content: (i, feat) => {
                 
                    return (
                        '<div style="width: 200px; height: 228px; padding: 0 0;">' +
                        '<p style="display: block; height:80px; line-height:80px;font-size:40px; border:4px solid rgba(1, 95, 217,0.6);background-color:rgba(1, 95, 217,0.6); color:#FFF; border-radius: 15px; text-align:center; margin:0; padding:5px;">' +
                        feat.properties.key + ':'
                    
                        + feat.properties.value
                        +
                        '</p><span style="width: 10px; height: 130px; margin: 0 auto; display: block; background-color:rgba(1, 95, 217,0.6);"></span></div>'
                    );
                },
                unit: 'meter',
                rotation: 0,
                alwaysFront: true,
                size: [40000, 70000 / 2],
                altitude: 0,
            });
            ScutcheonNumberLocal.add(zMarker);

        },

绘制不同行政区色块

 //添加行政区域区分
        administrativeRegion() {
            var that = this
            AMapUI.loadUI(
                ['geo/DistrictExplorer'],
                function (DistrictExplorer) {
                    initPage(DistrictExplorer)
                }
            )
            let initPage = (DistrictExplorer) => {
                let that = this
                //创建一个实例
                var districtExplorer = (this.districtExplorer =
                    new DistrictExplorer({
                        eventSupport: true,
                        map: that.map
                    }))
                // 内部区域被点击
                this.districtExplorer.on('featureClick', (e, feature) => {
                    this.interiorClice(feature)

                })
                // 外部区域被点击
                this.districtExplorer.on('outsideClick', (e) => {
                    this.externalClice()

                })
                //鼠标移入
                this.districtExplorer.on('featureMouseover', (e, feature) => {

                    this.toggleHoverFeature(feature, e.type === 'featureMouseover',
                        e.originalEvent ? e.originalEvent.lnglat : null);
                })
                //鼠标移出
                this.districtExplorer.on('featureMouseout', (e, feature) => {
                    this.toggleHoverfeatureMouseout(feature)
                })

                // // 绘制多区域
                this.loadMultiAreaNodes()
            }
        },
        ///渲染最大色块
        loadMultiAreaNodes() {
            var that = this
            let adcodes = [this.dyeMapAdcode]
            this.districtExplorer.loadMultiAreaNodes(
                adcodes,
                (error, areaNodes) => {
                    //设置定位节点,支持鼠标位置识别
                    //注意节点的顺序,前面的高优先级
                    this.districtExplorer.setAreaNodesForLocating(areaNodes)
                    //清除已有的绘制内容
                    this.districtExplorer.clearFeaturePolygons()
                    for (let i = 0, len = areaNodes.length; i < len; i++) {
                        this.renderAreaNode(areaNodes[i])
                    }
                }
            )
        },
  // 根据各行政区划的PQI之去绘制各区域的颜色
        renderAreaNode(areaNode) {
            var that = this
            // 绘制父区域
            that.districtExplorer.renderParentFeature(areaNode, {
                cursor: 'default',
                bubble: true,
                // strokeColor: "#00a4ce", //线颜色
                strokeColor: '#FFF000', //线颜色
                strokeOpacity: 1, // 线透明度,
                strokeWeight: 1.5, // 线宽
                fillColor: 'rgba(65,115,153,0.1)', //填充色
                // fillColor: '#fillColor',
                fillOpacity: 0 // 填充透明度
            })
            //绘制子级区域

            that.districtExplorer.renderSubFeatures(
                areaNode,
                function (feature, i) {
                    var fillOpacity = 0.5
                    if (that.boardType.typeOne || that.boardType.typeTwo) {
                        //如或是路网图   填充透明
                        fillOpacity = 0
                    }
//getDistrictColor就是返回个色值根据自己业务不同写自己的判断即可
                    var fillColor = that.getDistrictColor(feature)
                    return {
                        cursor: 'default',
                        bubble: true,
                        strokeColor: '#FFF000',
                        // strokeColor,
                        strokeOpacity: 1, // 线透明度
                        strokeWeight: 0.4, // 线宽
                        fillColor,
                        // fillColor: '#fff',
                        fillOpacity: fillOpacity// 填充透明度
                    }
                }
            )
            that.map.setFitView(that.districtExplorer.getAllFeaturePolygons())
        },

//鼠标移入路线后显示路线详情弹窗

    //初始化路线信息弹窗
        addSimpleInfoWindow(linData) {
          //linData为路线描述信息
            var content = [
                "<div class='linInfoWindow'>"
                + "<div class='linInfoWindow-head'>路线详情</div>"
                + "<div class='linInfoWindow-name'>" + linData.projectName + "</div>"
                + `<div class='linInfoWindow-body'> ${linData.briefIntroduction ? linData.briefIntroduction : '暂无详情数据'}  </div>`
                + "</div>"
            ];
            infoWindow = new AMap.InfoWindow({
                content: content,//传入 dom 对象,或者 html 字符串
                isCustom: true,
                offset: new AMap.Pixel(0, -20) //防止鼠标触碰窗体频闪
            });
        },

兴趣点添加

 //初始化标记点
        initMark(linData) {
//linData.projectPointList=替换为自己项目里的点位信息
            if (linData.projectPointList.length > 0) {
                linData.projectPointList.forEach(el => {
                    if (el.position) {
                        this.markerList.push(this.addMark(el))
                    }
                })
            }
        },
        //添加点位信息
        addMark(projectPoint) {
            // projectPoint:点位信息
            var xy = projectPoint.position.split(',')
            var that = this
            var content = `<div class='mapMark'>
                <div class='MarkName'>${that.markTypList[projectPoint.type].name}</div>
                <img img : src = "${that.getImg(projectPoint.type)}" ></img >
                </div>`;
            var marker = new AMap.Marker({
                content: content,  // 自定义点标记覆盖物内容
                position: [xy[0], xy[1]], // 基点位置
                offset: new AMap.Pixel(-40, -110) // 相对于基点的偏移位置 根据 mapWindow.less定
            });
            var markerContent = [
                "<div class='linInfoWindow'>"
                + "<div class='linInfoWindow-head'>点位详情</div>"
                + "<div class='linInfoWindow-name'>" + projectPoint.pointName + "</div>"
                + `<div class='linInfoWindow-body'>${projectPoint.baseInfo ? projectPoint.baseInfo : '暂无点位描述详情'}  </div>`
                + "</div>"
            ];
            var markerInfoWindow = new AMap.InfoWindow({
                content: markerContent,//传入 dom 对象,或者 html 字符串
                isCustom: true,
                offset: new AMap.Pixel(0, -20) //防止鼠标触碰窗体频闪
            });
            //  给兴趣点增加鼠标移入事件
            marker.on('mouseover', (e) => {
                markerInfoWindow.open(that.map, [e.lnglat.lng, e.lnglat.lat]);
            });
            // 给兴趣点增加 鼠标移出事件
            marker.on('mouseout', (e) => {
                markerInfoWindow.close();
            });
            // 给兴趣点增加 点击事件
            marker.on('click', function (e) {
                that.markerClick(e, projectPoint)
            })
            that.map.add(marker);
            return marker
        },
 //统一获取兴趣点图标
        getImg(type) {
            return require(`@/assets/image/map/mapIcon/${this.markTypList[type].iconName}.png`)
        },

// 下方为覆盖物的各种事件

  //内部点击
        interiorClice(feature) {
            if (this.currentArea.adcode != feature.properties.adcode) {
               
             
                this.dyeMapAdcode = feature.properties.adcode
                this.loadMultiAreaNodes()
                this.mapRemove()
            } else {
                console.log('不可下段');
            }
        },
        //外部点击
        externalClice() {
            if (this.currentArea.adcode != 411300) {
               
                this.dyeMapAdcode = 411300
              
                this.loadMultiAreaNodes()
                this.mapRemove()
            } else {
                console.log('不可升段');
            }
        },
        //色块鼠标移入高光
        toggleHoverFeature(feature, isHover, position) {
            var that = this
            if (!feature) {
//自己写的判断在路网图不染色根据自己业务改
                return;
            }
           
            var props = feature.properties;
            var polys = this.districtExplorer.findFeaturePolygonsByAdcode(props.adcode);
            for (var i = 0, len = polys.length; i < len; i++) {
                polys[i].setOptions({
                    fillOpacity: 0.8
                });
            }
        },
        //色块鼠标移出
        toggleHoverfeatureMouseout(feature) {
            var that = this
            if (!feature) {
                return;
            }
            if (this.singleProject || this.currentArea.adcode != 411300 || that.boardType.typeOne || that.boardType.typeTwo) {
                //如或是路网图不加颜色
                return;
            }
            var props = feature.properties;
            var polys = this.districtExplorer.findFeaturePolygonsByAdcode(props.adcode);
            for (var i = 0, len = polys.length; i < len; i++) {
                polys[i].setOptions({
                    fillOpacity: 0.5
                });
            }
        },
        //路线点击
        linClick(e, linData) {
            var that = this
          
            var index = Math.floor(linData.projectLineLists.length / 2)
            var xy = linData.projectLineLists[index]
            that.map.setZoomAndCenter(13, [xy.longitude, xy.latitude])
//地图放大定位中心点
        },
        //路线鼠标移入
        linMouseover(e, linData) {

//写自己业务 打开弹窗
             
            var that = this
            infoWindow.open(that.map, [e.lnglat.lng, e.lnglat.lat]);
        },
        //路线鼠标移出
        linMouseout(e, linData) {
            //关闭弹窗
            infoWindow.close();
        },
        //兴趣点点击
        markerClick(e, projectPoint) {
         
          
              //写自己业务
              //打开弹窗
         
                this.dialogTermvisible = true
          
        },
        //返回县级
        backCountyClick() {

          //写自己业务
         
        }

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
接入高德地图API实现公交路线查询并绘制路线图像,可以分为以下几个步骤: 1. 在高德开放平台注册账号,创建应用并获取API Key。 2. 在微信小程序中引入高德地图JS API。 3. 创建一个页面用于输入公交起和终,并实现搜索功能。 4. 在搜索结果中选择一个公交路线,获取其详细信息。 5. 使用高德地图JS API绘制公交路线图像。 下面是具体的实现步骤: 1. 在高德开放平台注册账号,创建应用并获取API Key。 首先需要在高德开放平台注册账号,然后创建一个应用,获取API Key。在创建应用时,需要选择Web服务类型,并勾选JavaScript API和Web服务API。注册完成后,在应用详情页面可以找到API Key。 2. 在微信小程序中引入高德地图JS API。 在微信小程序中引入高德地图JS API,可以通过在app.json中配置: ``` { "pages": [ "pages/index/index" ], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "WeChat", "navigationBarTextStyle": "black" }, "plugins": { "myPlugin": { "version": "1.0.0", "provider": "wxidxxxxxxxxxxxxxxxx" }, "myAmapPlugin": { "version": "1.0.0", "provider": "wxidxxxxxxxxxxxxxxxx", "options": { "key": "your_amap_api_key" } } } } ``` 其中,myAmapPlugin表示高德地图插件,options中的key为在高德开放平台上获取的API Key。 在需要使用高德地图的页面中,可以使用wx.createMapContext()方法创建一个地图上下文对象,从而可以调用高德地图JS API的方法。 3. 创建一个页面用于输入公交起和终,并实现搜索功能。 在小程序中创建一个页面,用于输入公交起和终,并实现搜索功能。可以使用微信小程序提供的组件,如input、button等,来实现页面布局和交互。 在搜索功能中,可以调用高德地图Web服务API中的公交路径规划接口,获取公交路线的详细信息。具体的调用方式可以参考高德地图Web服务API文档。 4. 在搜索结果中选择一个公交路线,获取其详细信息。 在搜索结果页面中,可以展示多条公交路线,用户可以选择其中一条路线进行查看。当用户选择一条路线时,需要获取该路线的详细信息,包括起、终、途经站、方案总距离和预计耗时等。 可以使用高德地图Web服务API中的公交路径规划接口返回的数据来展示公交路线的详细信息。 5. 使用高德地图JS API绘制公交路线图像。 当用户选择一条公交路线后,可以使用高德地图JS API的drawPolyline()方法绘制公交路线图像。首先需要获取该路线的途经站的经纬度坐标,在drawPolyline()方法中传入这些坐标,就可以绘制出公交路线图像。 具体的代码实现可以参考高德地图JS API文档中的示例代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值