441 lines
12 KiB
Vue
441 lines
12 KiB
Vue
<template>
|
|
<div class="app-container">
|
|
<div id="map-container" tabindex="0"></div>
|
|
<div v-show="flag" id="geo-tip"></div>
|
|
<div v-show="flag" id="panel"></div>
|
|
<div v-show="mapClick && flag" id='bt-wrapper'>
|
|
<div id='bt'>点击去高德地图</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script>
|
|
import AMap from 'AMap'
|
|
import moment from 'moment'
|
|
import { fetchDeviceList } from '@/api/counter'
|
|
export default {
|
|
name: 'map2',
|
|
data() {
|
|
return {
|
|
geo_map: null,
|
|
map: null,
|
|
flag: false,
|
|
mapClick: false,
|
|
geo: [],
|
|
defaultPositions: [
|
|
{
|
|
title: '海洋公园',
|
|
position: [114.186739, 22.250872],
|
|
count: 66,
|
|
signal: 23,
|
|
energy: 4333
|
|
},
|
|
{
|
|
title: '香港大学',
|
|
position: [114.149176, 22.286245],
|
|
count: 232,
|
|
signal: 23,
|
|
energy: 4333
|
|
},
|
|
{
|
|
title: '南丫北容村',
|
|
position: [114.126261, 22.22863],
|
|
count: 87,
|
|
signal: 33,
|
|
energy: 4333
|
|
},
|
|
{
|
|
title: '大帽山',
|
|
position: [114.124105, 22.401088],
|
|
count: 66,
|
|
signal: 23,
|
|
energy: 4223
|
|
},
|
|
{
|
|
title: '西贡郊野公园',
|
|
position: [114.370966, 22.411628],
|
|
count: 616,
|
|
signal: 23,
|
|
energy: 4556
|
|
},
|
|
{
|
|
title: '盐田',
|
|
position: [114.223565, 22.380771],
|
|
count: 66,
|
|
signal: 23,
|
|
energy: 4433
|
|
},
|
|
{
|
|
title: '清水',
|
|
position: [114.312821, 22.301076],
|
|
count: 2336,
|
|
signal: 23,
|
|
energy: 4333
|
|
}
|
|
]
|
|
}
|
|
},
|
|
methods: {
|
|
sleep(time) {
|
|
for (var temp = Date.now(); Date.now() - temp <= time;);
|
|
},
|
|
gotoDriving() {
|
|
this.$confirm('此操作将跳转到路线规划, 是否继续?', '提示', {
|
|
confirmButtonText: '确定',
|
|
cancelButtonText: '取消',
|
|
type: 'warning'
|
|
}).then(() => {
|
|
this.map.clearMap()
|
|
this.handleDrivingClick()
|
|
this.flag = true
|
|
this.$message({
|
|
type: 'success',
|
|
message: '跳转成功!'
|
|
})
|
|
}).catch(() => {
|
|
document.getElementById('map-container').removeEventListener
|
|
this.$message({
|
|
type: 'info',
|
|
message: '已取消跳转'
|
|
})
|
|
})
|
|
},
|
|
initMap() {
|
|
this.map = new AMap.Map('map-container', {
|
|
center: [114.143472, 22.284105],
|
|
resizeEnable: true,
|
|
zoom: 10
|
|
})
|
|
// 标记点测试数据
|
|
const markers = []
|
|
this.defaultPositions.forEach((item, index) => {
|
|
const marker = new AMap.Marker({
|
|
position: item.position,
|
|
zIndex: 10,
|
|
title: item.title,
|
|
map: this.map
|
|
})
|
|
const infoWindow = this.instantiateInforWindow(item)
|
|
AMap.event.addListener(marker, 'click', () => {
|
|
infoWindow.open(this.map, marker.getPosition())
|
|
})
|
|
markers.push(marker)
|
|
})
|
|
AMap.plugin(['AMap.ToolBar', 'AMap.Scale'], () => {
|
|
this.map.addControl(new AMap.ToolBar())
|
|
this.map.addControl(new AMap.Scale())
|
|
})
|
|
},
|
|
createInfoWindow(title, content) {
|
|
const info = document.createElement('div')
|
|
info.className = 'info'
|
|
info.setAttribute('id', 'info')
|
|
|
|
// 可以通过下面的方式修改自定义窗体的宽高
|
|
// info.style.width = "400px"
|
|
// 定义顶部标题
|
|
const top = document.createElement('div')
|
|
const titleD = document.createElement('div')
|
|
titleD.className = 'title'
|
|
const closeX = document.createElement('img')
|
|
top.className = 'info-top'
|
|
titleD.innerHTML = title
|
|
closeX.src = 'https://webapi.amap.com/images/close2.gif'
|
|
closeX.onclick = this.closeInfoWindow
|
|
|
|
top.appendChild(titleD)
|
|
top.appendChild(closeX)
|
|
info.appendChild(top)
|
|
|
|
// 定义中部内容
|
|
const middle = document.createElement('div')
|
|
middle.className = 'info-middle'
|
|
middle.innerHTML = content
|
|
info.appendChild(middle)
|
|
|
|
// 定义底部内容
|
|
const bottom = document.createElement('div')
|
|
bottom.className = 'info-bottom'
|
|
bottom.style.position = 'relative'
|
|
bottom.style.top = '0px'
|
|
bottom.style.margin = '0 auto'
|
|
const sharp = document.createElement('img')
|
|
sharp.src = 'https://webapi.amap.com/images/sharp.png'
|
|
bottom.appendChild(sharp)
|
|
info.appendChild(bottom)
|
|
// console.log(info)
|
|
return info
|
|
},
|
|
instantiateInforWindow(item, drivingButton = false) {
|
|
const content = []
|
|
const curTime = moment().format('YYYY-MM-DD HH:mm:ss')
|
|
content.push('<div class="count">' + item.count + '</div>')
|
|
content.push('<div class="time">时间:' + curTime + '</div>')
|
|
content.push('<div><span>信号:' + item.signal + '</span><span class="energy">电量:' + item.energy + '</span></div>')
|
|
content.push('<div>区域:尖沙咀</div>\n')
|
|
if (drivingButton) {
|
|
content.push('<button id="btn" class="btn">驾车规划</button>')
|
|
}
|
|
const infoWindow = new AMap.InfoWindow({
|
|
isCustom: true,
|
|
closeWhenClickMap: true,
|
|
content: this.createInfoWindow(item.title, content.join('\n')),
|
|
offset: new AMap.Pixel(16, -45)
|
|
})
|
|
return infoWindow
|
|
},
|
|
closeInfoWindow() {
|
|
this.map.clearInfoWindow()
|
|
},
|
|
onComplete(data) {
|
|
var str = ['定位成功']
|
|
this.geo = [data.position.getLng(), data.position.getLat()]
|
|
str.push(data.position.getLng())
|
|
str.push(data.position.getLat())
|
|
console.log(this.geo)
|
|
if (data.accuracy) {
|
|
str.push('精度:' + data.accuracy + ' 米')
|
|
}
|
|
str.push('是否经过偏移:' + (data.isConverted ? '是' : '否'))
|
|
document.getElementById('geo-tip').innerHTML = str.join('<br>')
|
|
const drivingOptions = {
|
|
map: this.geo_map,
|
|
panel: 'panel'
|
|
}
|
|
// 驾车路径规划
|
|
const driving = new AMap.Driving(drivingOptions)
|
|
const button = document.getElementById('bt')
|
|
driving.search(new AMap.LngLat(data.position.getLng(), data.position.getLat()), new AMap.LngLat(113.203460828994, 22.64902452257), (status, result) => {
|
|
button.onclick = () => {
|
|
driving.searchOnAMAP({
|
|
origin: result.origin,
|
|
destination: result.destination
|
|
})
|
|
}
|
|
})
|
|
},
|
|
onError() {
|
|
document.getElementById('geo-tip').innerHTML = '定位失败'
|
|
},
|
|
handleDrivingClick() {
|
|
const geo_map = new AMap.Map('map-container')
|
|
this.geo_map = geo_map
|
|
AMap.plugin(['AMap.ToolBar', 'AMap.Scale', 'AMap.Driving', 'AMap.Geolocation'], () => {
|
|
geo_map.addControl(new AMap.ToolBar())
|
|
geo_map.addControl(new AMap.Scale())
|
|
const geolocation = new AMap.Geolocation({
|
|
enableHighAccuracy: true,
|
|
timeout: 10000,
|
|
buttonOffset: new AMap.Pixel(10, 20),
|
|
zoomToAccuracy: true,
|
|
panToLocation: false,
|
|
buttonPosition: 'RB'
|
|
})
|
|
geo_map.addControl(geolocation)
|
|
geolocation.getCurrentPosition()
|
|
// 返回定位信息
|
|
AMap.event.addListener(geolocation, 'complete', this.onComplete)
|
|
// 返回定位出错信息
|
|
AMap.event.addListener(geolocation, 'error', this.onError)
|
|
})
|
|
},
|
|
gotoDrivingEvent(e) {
|
|
const event = e || window.event
|
|
const target = event.target || event.srcElement
|
|
if (target.nodeName.toLocaleLowerCase() === 'button') {
|
|
console.log('the content is: ', target.innerHTML)
|
|
this.gotoDriving()
|
|
}
|
|
},
|
|
getDevicesInitMap(params) {
|
|
fetchDeviceList(params).then(response => {
|
|
this.deviceList = response.data.results
|
|
const item = this.deviceList[0]
|
|
this.coordinate = item.coordinate
|
|
if (this.coordinate === null) {
|
|
this.coordinate = [113.203460828994, 22.64902452257]
|
|
}
|
|
this.map = new AMap.Map('map-container', {
|
|
center: this.coordinate,
|
|
resizeEnable: true,
|
|
zoom: 15
|
|
})
|
|
const marker = new AMap.Marker({
|
|
position: this.coordinate,
|
|
zIndex: 10,
|
|
map: this.map
|
|
})
|
|
|
|
item.title = '东岸灯饰工业区'
|
|
const infoWindow = this.instantiateInforWindow(item, true)
|
|
AMap.event.addListener(marker, 'click', () => {
|
|
infoWindow.open(this.map, marker.getPosition())
|
|
})
|
|
// this.handleDrivingClick()
|
|
// this.flag = true
|
|
// if (AMap.UA.mobile) {
|
|
// document.getElementById('panel').style.display = 'none'
|
|
// // document.getElementById('bt-wrapper').style.display = 'normal'
|
|
// }
|
|
})
|
|
const mapContainer = document.getElementById('map-container')
|
|
mapContainer.addEventListener('click', this.gotoDrivingEvent)
|
|
}
|
|
},
|
|
created() {
|
|
this.mapClick = this.$store.getters.mapClick
|
|
// if (!this.mapClick) {
|
|
// this.flag = true
|
|
// }
|
|
},
|
|
mounted() {
|
|
if (this.mapClick) {
|
|
const device_id = this.$store.getters.deviceID
|
|
// this.sleep(2000)
|
|
this.getDevicesInitMap({ device_id })
|
|
} else {
|
|
this.initMap()
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<style lang="css">
|
|
.app-container {
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
#map-container {
|
|
width: 100%;
|
|
height: 90%;
|
|
overflow: hidden;
|
|
position: absolute;
|
|
}
|
|
.info {
|
|
border: solid 1px silver;
|
|
}
|
|
div.info-top {
|
|
position: relative;
|
|
background: none repeat scroll 0 0 #F9F9F9;
|
|
border-bottom: 1px solid #CCC;
|
|
border-radius: 5px 5px 0 0;
|
|
}
|
|
div.info-top .title {
|
|
display: inline-block;
|
|
color: rgb(236, 110, 37);
|
|
font-size: 14px;
|
|
font-weight: bold;
|
|
line-height: 31px;
|
|
padding: 0 10px;
|
|
}
|
|
div.info-top img {
|
|
position: absolute;
|
|
top: 10px;
|
|
right: 10px;
|
|
transition-duration: 0.25s;
|
|
}
|
|
div.info-top img:hover {
|
|
box-shadow: 0px 0px 5px #000;
|
|
}
|
|
div.info-middle {
|
|
font-size: 12px;
|
|
padding: .4rem .8rem .5rem .5rem;
|
|
line-height: 20px;
|
|
background-color: white;
|
|
}
|
|
.info-middle .count {
|
|
display: flex;
|
|
justify-content: flex-end;
|
|
font-size: 1.5rem;
|
|
font-weight: bold;
|
|
color: #666;
|
|
margin: 0 0 .4rem 0;
|
|
}
|
|
.info-middle span.energy {
|
|
margin-left: 1.8rem;
|
|
}
|
|
div.info-bottom {
|
|
height: 0px;
|
|
width: 100%;
|
|
clear: both;
|
|
text-align: center;
|
|
}
|
|
div.info-bottom img {
|
|
position: relative;
|
|
z-index: 104;
|
|
}
|
|
#panel {
|
|
position: absolute;
|
|
background-color: white;
|
|
max-height: 80%;
|
|
overflow-y: auto;
|
|
top: 10px;
|
|
right: 10px;
|
|
width: 250px;
|
|
border: solid 1px silver;
|
|
margin-top: 100px;
|
|
}
|
|
@media only screen and (max-width: 768px) {
|
|
#panel {
|
|
display: none;
|
|
}
|
|
.el-message-box {
|
|
width: 100%;
|
|
}
|
|
}
|
|
#geo-tip {
|
|
font-size: .8rem;
|
|
position: absolute;
|
|
padding: 5px;
|
|
color: white;
|
|
background-color:goldenrod;
|
|
width: 150px;
|
|
top: 15%;
|
|
left: 10%;
|
|
border-radius: 5px;
|
|
text-align: center;
|
|
}
|
|
#bt-wrapper {
|
|
position: absolute;
|
|
width: 2rem;
|
|
bottom: 15%;
|
|
right: 10rem;
|
|
}
|
|
#bt {
|
|
font-size: .8rem;
|
|
text-align: center;
|
|
cursor: pointer;
|
|
border-radius: 5px;
|
|
background-color: #0D9BF2;
|
|
padding: 6px;
|
|
width: 160px;
|
|
color: white;
|
|
margin: 0 auto;
|
|
}
|
|
#btn {
|
|
display: inline-block;
|
|
line-height: 1;
|
|
white-space: nowrap;
|
|
cursor: pointer;
|
|
background: #fff;
|
|
border: 1px solid #dcdfe6;
|
|
border-color: #dcdfe6;
|
|
color: #606266;
|
|
-webkit-appearance: none;
|
|
text-align: center;
|
|
box-sizing: border-box;
|
|
outline: none;
|
|
margin: 0;
|
|
transition: .1s;
|
|
font-weight: 500;
|
|
padding: 5px 5px;
|
|
font-size: 12px;
|
|
border-radius: 2px;
|
|
}
|
|
#btn:focus, #btn:hover {
|
|
color: #409eff;
|
|
border-color: #c6e2ff;
|
|
background-color: #ecf5ff;
|
|
}
|
|
</style>
|