百度地图使用流程
首先注册一下账号和自己的ak
开始使用百度地图api开发
<div id="map"></div>
<script type="text/javascript" src="https://api.map.baidu.com/api?v=1.0&&type=webgl&ak=你申请的ak">
</script>
<script>
const map = new BMapGL.Map('map')
const point = new BMapGL.Point(114.611458, 36.660914)
map.centerAndZoom(point, 16)
map.enableScrollWheelZoom()
</script>
地图设置类型
map.setMapType(BMAP_NORMAL_MAP)
地图异步加载
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
html {
height: 100%
}
body {
height: 100%;
margin: 0px;
padding: 0px
}
#map {
width: 900px;
height: 450px;
border: 1px solid #ccc;
margin: 20px auto;
}
</style>
</head>
<body>
<div id="map"></div>
</body>
<!-- <script type="text/javascript" src="https://api.map.baidu.com/api?v=1.0&&type=webgl&ak=GARHEjrSEtihCn5PY0TpvMBYdCa02TOq"> -->
<!-- </script> -->
<script>
window.onload = loadScript
function loadScript() {
const script = document.createElement('script')
script.src = 'https://api.map.baidu.com/api?v=1.0&&type=webgl&ak=GARHEjrSEtihCn5PY0TpvMBYdCa02TOq&callback=initmap'
document.body.appendChild(script)
}
function initmap() {
const map = new BMapGL.Map('map')
const point = new BMapGL.Point(116.211596, 40.143386)
map.centerAndZoom(point, 18)
map.enableScrollWheelZoom()
map.setMapType(BMAP_NORMAL_MAP)
}
</script>
</html>
地图旋转与倾斜
map.setTilt()
map.setHeading(i)
地图常用控件
<script>
const map = new BMapGL.Map('map')
const point = new BMapGL.Point(116.404137, 39.914939)
map.centerAndZoom(point, 18)
map.enableScrollWheelZoom()
const zoomCtrl = new BMapGL.ZoomControl({
anchor: BMAP_ANCHOR_TOP_LEFT,
offset: new BMapGL.Size(10, 15)
})
map.addControl(zoomCtrl)
const scaleCtrl = new BMapGL.ScaleControl({
anchor: BMAP_ANCHOR_TOP_RIGHT,
offset: new BMapGL.Size(10, 15)
})
map.addControl(scaleCtrl)
</script>
添加折线,图像标记,信息窗口
<script>
const map = new BMapGL.Map('map')
const point = new BMapGL.Point(116.215286, 40.142213)
map.centerAndZoom(point, 18)
map.enableScrollWheelZoom()
const zoomCtrl = new BMapGL.ZoomControl({
anchor: BMAP_ANCHOR_TOP_LEFT,
offset: new BMapGL.Size(10, 15)
})
map.addControl(zoomCtrl)
const scaleCtrl = new BMapGL.ScaleControl({
anchor: BMAP_ANCHOR_TOP_RIGHT,
offset: new BMapGL.Size(10, 15)
})
map.addControl(scaleCtrl)
const url = 'https://m.360buyimg.com/babel/jfs/t1/57520/8/375/4092/5cd4f1d8Ea1266047/1c590322ece537ba.png'
const icon = new BMapGL.Icon(url, new BMapGL.Size(30, 30), {
anchor: new BMapGL.Size(0, 0),
imageOffset: new BMapGL.Size(0, 0),
})
const marker = new BMapGL.Marker(point, {
icon: icon
})
map.addOverlay(marker)
const pointArr = [
new BMapGL.Point(116.215735, 40.141989),
new BMapGL.Point(116.215156, 40.142696),
new BMapGL.Point(116.214594, 40.142472),
new BMapGL.Point(116.215043, 40.141789),
];
const options = {
strokeColor: "#f00",
strokeWeight: 10,
strokeOpacity: 0.3,
}
const pl = new BMapGL.Polygon(pointArr, options)
map.addOverlay(pl)
const lab = new BMapGL.Label('积云教育', {
offset: new BMapGL.Size(-30, -20),
position: point
})
lab.setStyle({
border: '1px dashed #00f',
padding: '6px 4px',
background: "#f00",
color: "#ff0"
})
map.addOverlay(lab)
const ohtml = `
<div>
<h4>欢迎来到积云教育学习高薪就业课程</h4>
<img style='width:30%' src='https://t7.baidu.com/it/u=1951548898,3927145&fm=193&f=GIF' />
</div>
`
const infoW = new BMapGL.InfoWindow(ohtml, {
title: '欢迎!',
width: 300,
height: 200,
offset: new BMapGL.Size(0, 0)
})
lab.addEventListener('click', function() {
map.openInfoWindow(infoW, point)
}, false)
</script>
视角动画
<script>
const map = new BMapGL.Map('map')
const point = new BMapGL.Point(116.403963, 39.915119)
map.centerAndZoom(point, 18)
map.enableScrollWheelZoom()
map.setTilt(40)
map.setHeading(50)
const dhArr = [{
center: new BMapGL.Point(116.307092, 40.054922),
zoom: 20,
tilt: 50,
heading: 0,
percentage: 0
}, {
center: new BMapGL.Point(116.307631, 40.055391),
zoom: 21,
tilt: 70,
heading: 0,
percentage: 0.1
}, {
center: new BMapGL.Point(116.306858, 40.057887),
zoom: 21,
tilt: 70,
heading: 0,
percentage: 0.25
}, {
center: new BMapGL.Point(116.306858, 40.057887),
zoom: 21,
tilt: 70,
heading: -90,
percentage: 0.35
}, {
center: new BMapGL.Point(116.307904, 40.058118),
zoom: 21,
tilt: 70,
heading: -90,
percentage: 0.45
}, {
center: new BMapGL.Point(116.307904, 40.058118),
zoom: 21,
tilt: 70,
heading: -180,
percentage: 0.55
}, {
center: new BMapGL.Point(116.308902, 40.055954),
zoom: 21,
tilt: 70,
heading: -180,
percentage: 0.75
}, {
center: new BMapGL.Point(116.308902, 40.055954),
zoom: 21,
tilt: 70,
heading: -270,
percentage: 0.85
}, {
center: new BMapGL.Point(116.307779, 40.055754),
zoom: 21,
tilt: 70,
heading: -360,
percentage: 0.95
}, {
center: new BMapGL.Point(116.307092, 40.054922),
zoom: 20,
tilt: 50,
heading: -360,
percentage: 1
}, ]
const options = {
delay: 0,
duration: 8000,
interation: 1
}
const dh = new BMapGL.ViewAnimation(dhArr, options)
map.startViewAnimation(dh)
</script>
轨迹动画
<script>
const map = new BMapGL.Map('map')
const point = new BMapGL.Point(116.403963, 39.915119)
map.centerAndZoom(point, 16)
map.enableScrollWheelZoom()
const plArr = [
new BMapGL.Point(116.380597, 39.913314),
new BMapGL.Point(116.413008, 39.91431),
new BMapGL.Point(116.413224, 39.921504),
new BMapGL.Point(116.424363, 39.921615),
new BMapGL.Point(116.424578, 39.914421),
]
const pl = new BMapGL.Polyline(plArr)
const track = new BMapGLLib.TrackAnimation(map, pl, {
overallView: true,
tilt: 30,
duration: 20000,
delay: 2000
})
track.start()
</script>
在vue中如何使用地图
vue 2的官方文档
vue3的官方文档
vue3百度地图布局
安装地图依赖
npm install vue-baidu-map-3x --save
注册为全局百度地图
import { createApp } from 'vue'
import App from './App.vue'
import BaiduMap from 'vue-baidu-map-3x'
const app = createApp(App);
app.use(BaiduMap, {
ak: '百度地图ak',
});
app.mount('#app');
使用百度地图组件
<script setup lang="ts"></script>
<template>
<div class="home">
<baidu-map class="bm-view" center="天安门" :zoom="15">
<bm-scale anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-scale>
<bm-label
content="我爱北京天安门"
:position="{ lng: 116.404, lat: 39.915 }"
:labelStyle="{ color: 'red', fontSize: '24px' }"
title="Hover me"
/>
</baidu-map>
<!-- 116.211596,40.143386 -->
</div>
</template>
<style scoped>
.home {
width: 100vw;
height: 100vh;
background: #ccc;
}
.bm-view {
width: 100%;
height: 100%;
}
</style>