vue调用GoogleMap绘制车辆行驶路线以及坐标点标记

配置

1.使用google map api v3版本,v3取消了marker的label方法,所以要自己定义叠加层来使用带文字标签的标注,但是自定义非常繁琐,故引用现成的markerwidthlabel.js即可实现对标注添加label。

查看

引入api步骤

项目配置

在index.html中引入script

<script type="text/javascript" src="https://maps.google.com/maps/api/js?key=你的key"></script>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="./markerwithlabel.js"></script>

绘制路线

tripMap(indexeventId?: number) {
        var latlng = new google.maps.LatLng(this.mapCenterLat, this.mapCenterLng);
        var myOptions = {
            zoom: 15, // 缩放级别
            center: latlng, // 坐标
            mapTypeId: google.maps.MapTypeId.ROADMAP // 类型:默认的普通二维图块
        };
        const directionsRenderer = new google.maps.DirectionsRenderer();
        var map = new google.maps.Map(document.getElementById('map'), myOptions);
        var routes = new google.maps.DirectionsService();
        directionsRenderer.setMap(map);
        // 线条设置
        var polyOptions = {
            strokeColor: '#3388FF', // 颜色
            strokeOpacity: 0.8, // 透明度
            strokeWeight: 5 // 宽度
        };
        var poly = new google.maps.Polyline(polyOptions);
        poly.setMap(map); // 装载
        /* 循环标出所有坐标 */
        let infowindow = new google.maps.InfoWindow({
            content: ''
        });
        let symbolicon: any = {
            path:
                'M 5.51856 0.910213 L 0.714103 16.1773 C 0.664367 16.3354 0.752169 16.5038 0.910214 16.5536 C 0.939352 16.5627 0.96972 16.5674 1.00027 16.5674 L 10.5672 16.5674 C 10.7329 16.5674 10.8672 16.4331 10.8672 16.2674 C 10.8672 16.2371 10.8626 16.207 10.8536 16.1781 L 6.09112 0.91093 C 6.04178 0.752761 5.87356 0.664538 5.71539 0.713878 C 5.62159 0.743139 5.54806 0.816483 5.51856 0.910213 Z',
            fillOpacity: 1,
            strokeColor: '#4A90E2',
            fillColor: '#FFCB41',
            fillRule: 'evenodd',
            strokeWeight: 1.4,
            scale: 1,
            rotation: 0,
            anchor: new google.maps.Point(6.3, 0)
        };
        var bounds = new google.maps.LatLngBounds(); // 声明一个bounds
        for (var i = 0; i < this.locations.length; i++) {
            var loc = this.locations[i].coordiate.split(',');
            var path = poly.getPath(); // 获取线条的坐标
            path.push(new google.maps.LatLng(loc[0], loc[1])); // 为线条添加标记坐标
            bounds.extend(new google.maps.LatLng(loc[0], loc[1]));
            // 生成标记图标
            symbolicon.rotation = Number(this.locations[i].heading);
            const marker = new google.maps.Marker({
                position: new google.maps.LatLng(loc[0], loc[1]),
                map: map,
                icon: this.locations[i].eventId ? this.eventicon : symbolicon
                // 测试用
                // icon: symbolicon
            });
            let item = this.locations[i];
            let eventId = item.eventId;
            let duration = 0;
            if (eventId) {
                console.log({ tripEventData: this.tripEventData });
                this.tripEventData.forEach((items: any) => {
                    if (items.id === eventId) {
                        duration = items.duration / 1000;
                    }
                });
                console.log('open');

                marker.addListener('click', () => {
                    infowindow.setContent(
                        `<div class='infowindows'>` +
                            `<div class='leftBar'>` +
                            `</div>` +
                            `<div class='content'>` +
                            `<div class='type'>${item.eventType}</div>` +
                            `<div class='time'>${this.defaultTimeValue(item.time)}</div>` +
                            `<div class='duration'>Video length:${duration}s</div>` +
                            '</div>' +
                            `<div οnclick=handlePlayVideo(${eventId})> ` +
                            `<img  src='https://data.waylens.com/a50ea735abd819a6d229b55431be7da0.svg' alt=''>` +
                            `</div>` +
                            `</div>`
                    );
                    infowindow.open({
                        anchor: marker,
                        map,
                        shouldFocus: true
                    });
                });
            }
            if (indexeventId === eventId) {
                this.tripEventData.forEach((items: any) => {
                    if (items.id === indexeventId) {
                        duration = items.duration / 1000;
                    }
                });
                console.log('indexeventId === eventId');
                infowindow.setContent(
                    `<div class='infowindows'>` +
                        `<div class='leftBar'>` +
                        `</div>` +
                        `<div class='content'>` +
                        `<div class='type'>${item.eventType}</div>` +
                        `<div class='time'>${this.defaultTimeValue(item.time)}</div>` +
                        `<div class='duration'>Video length:${duration}s</div>` +
                        '</div>' +
                        `<div οnclick=handlePlayVideo(${eventId})> ` +
                        `<img  src='https://data.waylens.com/a50ea735abd819a6d229b55431be7da0.svg' alt=''>` +
                        `</div>` +
                        `</div>`
                );
                infowindow.open({
                    anchor: marker,
                    map,
                    shouldFocus: false
                });
            }
        }
        if (this.locations.length > 1) {
            // 如果坐标点多余1个,则使用  google map 的fitBounds,自动计算zoom值
            map.fitBounds(bounds);
        }
    }

单个坐标点

initMap(item: any, index: number) {
        this.isActive = index;
        let map: any;
        let latlng = { lat: item.gpsLatitude, lng: item.gpsLongitude };
        let myOptions = {
            zoom: this.centerZoom, // 缩放级别
            center: latlng, // 坐标
            mapTypeId: google.maps.MapTypeId.ROADMAP // 类型:默认的普通二维图块
        };
        let infowindow = new google.maps.InfoWindow({
            content: ''
        });
        map = new google.maps.Map(document.getElementById('map'), myOptions);
        const beachMarker = new google.maps.Marker({
            position: latlng,
            map,
            icon: this.eventicon
        });
        infowindow.setContent(
            `<div class='infowindows'>` +
                `<div class='leftBar'>` +
                `</div>` +
                `<div class='content'>` +
                `<div class='type'>${item.eventType}</div>` +
                `<div class='time'>${this.defaultTimeValue(item.gpsTime)}</div>` +
                `<div class='duration'>Video length:${item.duration / 1000}s</div>` +
                '</div>' +
                `<div οnclick=handlePlayVideo(${item.id})> ` +
                `<img  src='https://data.waylens.com/a50ea735abd819a6d229b55431be7da0.svg' alt=''>` +
                `</div>` +
                `</div>`
        );
        infowindow.open({
            anchor: beachMarker,
            map,
            shouldFocus: false
        });
    }

多个坐标点

initMaps() {
        let map: any;
        let lat = this.mapCenterLat;
        let lng = this.mapCenterLng;
        let latlng = { lat, lng };
        console.log({ latlng });
        let myOptions = {
            zoom: this.centerZoom, // 缩放级别
            center: latlng, // 坐标
            mapTypeId: google.maps.MapTypeId.ROADMAP // 类型:默认的普通二维图块
        };
        map = new google.maps.Map(document.getElementById('map'), myOptions);
        var bounds = new google.maps.LatLngBounds(); // 声明一个bounds
        // 提取每一个item的gpsData,都为空说明本页数据没有坐标返回,需要修正地图定位
        let gpsDataArray: any = [];
        this.dataList.forEach((item: any) => {
            if (item.gpsData) {
                gpsDataArray.push(item.gpsData);
                const infowindow = new google.maps.InfoWindow({
                    content: `<div id="content">${item.plateNo}-${item.driverName}` + `<div id="siteNotice">` + `<img style='width:400px;height:300px' src=${this.wrapperImage(item.lastPicture)} alt=''>` + '</div>' + '</div>'
                });
                let symbol: string = '';
                if (item.isOnline && item.mode === 'parking') {
                    symbol = this.greencar;
                } else if (item.isOnline && item.mode === 'driving') {
                    symbol = this.bulecar;
                } else if (!item.isOnline) {
                    symbol = this.officon;
                }
                this.cariconSymbol = symbol;
                let positions;
                if (item.gpsData.coordinate) {
                    positions = { lat: Number(item.gpsData.coordinate[1]), lng: Number(item.gpsData.coordinate[0]) };
                    bounds.extend(new google.maps.LatLng(Number(item.gpsData.coordinate[1]), Number(item.gpsData.coordinate[0])));
                } else {
                    positions = { lat: this.mapCenterLat, lng: this.mapCenterLng };
                    console.log({ positions });
                }
                // console.log({ positions });
                const beachMarker = new google.maps.Marker({
                    position: positions,
                    map,
                    icon: this.cariconSymbol
                });
                beachMarker.addListener('click', (item: any) => {
                    // this.cardetailDio = true;
                    this.handleTripDetail(item.cameraSn);
                });
                beachMarker.addListener('mouseover', () => {
                    infowindow.open({
                        anchor: beachMarker,
                        map,
                        shouldFocus: false
                    });
                });
                beachMarker.addListener('mouseout', () => {
                    infowindow.close();
                });
            }
        });
        console.log({ gpsDataArray });
        if (this.dataList.length > 1 && gpsDataArray.length > 1) {
            // 如果坐标点多余1个,则使用  google map 的fitBounds,自动计算zoom值
            map.fitBounds(bounds);
        }
    }

单个坐标点

initMap(item: any, index: number) {
        this.isActive = index;
        let map: any;
        let latlng = { lat: item.gpsLatitude, lng: item.gpsLongitude };
        let myOptions = {
            zoom: this.centerZoom, // 缩放级别
            center: latlng, // 坐标
            mapTypeId: google.maps.MapTypeId.ROADMAP // 类型:默认的普通二维图块
        };
        let infowindow = new google.maps.InfoWindow({
            content: ''
        });
        map = new google.maps.Map(document.getElementById('map'), myOptions);
        const beachMarker = new google.maps.Marker({
            position: latlng,
            map,
            icon: this.eventicon
        });
        infowindow.setContent(
            `<div class='infowindows'>` +
                `<div class='leftBar'>` +
                `</div>` +
                `<div class='content'>` +
                `<div class='type'>${item.eventType}</div>` +
                `<div class='time'>${this.defaultTimeValue(item.gpsTime)}</div>` +
                `<div class='duration'>Video length:${item.duration / 1000}s</div>` +
                '</div>' +
                `<div οnclick=handlePlayVideo(${item.id})> ` +
                `<img  src='https://data.waylens.com/a50ea735abd819a6d229b55431be7da0.svg' alt=''>` +
                `</div>` +
                `</div>`
        );
        infowindow.open({
            anchor: beachMarker,
            map,
            shouldFocus: false
        });
    }

效果图

细节分析(缩放和中心点尚需完善)

1.地图的中心点
	根据返回的经纬度均值取
	let max1 = Math.max(...coordinate1);
    let min1 = Math.min(...coordinate1);
    let max2 = Math.max(...coordinate2);
    let min2 = Math.min(...coordinate2);
    this.mapCenterLat = (max2 + min2) / 2;
    this.mapCenterLng = (max1 + min1) / 2;
2.地图的缩放系数(保证所有的坐标点都在图中展示开)
在循环坐标之前:
var bounds = new google.maps.LatLngBounds(); // 声明一个bounds
循环坐标之后
if (this.locations.length > 1) {
 // 如果坐标点多余1个,则使用  google map 的fitBounds,自动计算zoom值
    map.fitBounds(bounds);
 }
 3.修正icon的精准度
 
 


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在Vue项目中使用百度地图进行绘制,你可以按照以下步骤进行操作: 1. 首先,在你的Vue项目中安装 vue-baidu-map 插件。可以通过以下命令进行安装: ``` npm install vue-baidu-map --save ``` 2. 在你的Vue组件中引入 vue-baidu-map: ```vue <template> <div> <baidu-map :ak="yourBaiduMapAK" v-if="loaded"></baidu-map> </div> </template> <script> import { BaiduMap } from 'vue-baidu-map' export default { components: { BaiduMap }, data() { return { yourBaiduMapAK: 'yourBaiduMapAK', loaded: false } }, mounted() { // 在mounted钩子函数中设置地图加载完成后的回调函数 this.$nextTick(() => { this.loaded = true this.initMap() }) }, methods: { initMap() { // 初始化地图 // 创建地图实例 let map = new BMap.Map("map-container") // 设置地图中心 let point = new BMap.Point(116.404, 39.915) map.centerAndZoom(point, 15) // 创建标注物 let marker = new BMap.Marker(point) // 将标注物添加到地图中 map.addOverlay(marker) } } } </script> ``` 3. 在 `yourBaiduMapAK` 的位置,替换为你自己的百度地图开发者AK(API Key),可以在百度地图开放平台申请获取。 4. 在 `initMap` 方法中,可以根据需要设置地图的中心和缩放级别,然后创建标注物,并将其添加到地图中。 5. 在页面中添加一个容器,用于渲染地图: ```html <div id="map-container"></div> ``` 这样,你就可以在Vue项目中使用 vue-baidu-map 插件进行绘制了。记得替换 `yourBaiduMapAK` 为你自己的百度地图开发者AK。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值