前言: 高德地图文档很少,没有案例,又想学习一下高德地图基本使用 此致敬礼 记录一下 文档地址:https://elemefe.github.io/vue-amap
直接看最后推荐
引入 方式一:全局引入
npm install -S vue-amap
main.js写入
import VueAMap from 'vue-amap';
Vue.use(VueAMap);
VueAMap.initAMapApiLoader({
key: 'YOUR_KEY',
plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor'],
v: '1.4.4'
});
页面写入
<template>
<div class="amap-page-container">
<div :style="{width:'100%',height:'800px'}">
<el-amap vid="amap" :plugin="plugin" class="amap-demo" rotateEnable="true" :center="center">
</el-amap>
</div>
</div>
</template>
<script type="text/javascript">
export default {
data() {
const self = this;
return {
center: [121.59996, 31.197646],
plugin: [{
pName: "MapType",
defaultType: 1,
events: {
init(o) {
console.log(o);
},
},
}]
}
},
methods: {}
}
</script>
就可以得到一个简单的页面 方式二 按需引入
npm install -S vue-amap
页面写入
<template>
<div id="container" style="width: 100%; height: 100%; resize: both"></div>
</template>
<script>
export default {
data() {
return {
map: null,
};
},
async created() {
this.initMap();
},
methods: {
initMap() {
let AMap = window.AMap;
let _this = this;
_this.map = new AMap.Map("container", {
zooms: [1, 19.9],
center: [106.323597, 29.516401],
});
},
},
};
</script>
<style>
#container {
height: 100%;
}
</style>
你也可以得到一个地图
插件: 文档地址:https://elemefe.github.io/vue-amap/#/zh-cn/plugins/base 谷歌官方插件库:https://lbs.amap.com/api/javascript-api/guide/abc/plugins/
"AMap.Autocomplete",
"AMap.PlaceSearch",
"AMap.Scale",
"AMap.OverView",
"AMap.ToolBar",
"AMap.MapType",
"AMap.PolyEditor",
"AMap.CircleEditor",
"AMap.Geolocation"
使用
<el-amap vid="amap" :plugin="plugin" class="amap-demo" rotateEnable="true" :center="center">
zoom:16,
center:[121.406051,31.179695],
plugin:[{pName:'MapType'}]
贼菜 理解错了 自我感觉比较有用的链接 地图控件(官方):https://lbs.amap.com/api/javascript-api/reference/map-control 例: main.js
import VueAMap from 'vue-amap';
Vue.use(VueAMap);
VueAMap.initAMapApiLoader({
key: 'yourkey',
plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor','AMap.Buildings'],
v: '1.4.4'
});
页面
<template>
<div class="amap-page-container">
<div :style="{width:'100%',height:'800px'}">
<el-amap vid="amap" :plugin="plugin" class="amap-demo" rotateEnable="true" :center="center">
</el-amap>
</div>
</div>
</template>
<script type="text/javascript">
export default {
data() {
const self = this;
let AMap = window.AMap;
return {
center: [121.59996, 31.197646],
plugin: [{
pName: "MapType",
defaultType: 1,
showTraffic:true,
showRoad:true,
events: {
init(o) {
o.hide();
o.show();
},
},
}]
}
},
methods: {}
}
</script>
官网html写法
mapObj = new AMap.Map("imap",{
center:new AMap.LngLat(116.368904,39.913423),
zoom:16
});
mapObj.plugin(["AMap.MapType"],function(){
var type= new AMap.MapType({
defaultType:1
});
mapObj.addControl(type);
});
另一种写法 推荐!!!
index.html
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=yourkey"></script>
vue.config.js引入
module.exports = {
configureWebpack: {
externals: {
'AMap': 'AMap'
}
}
}
页面
<template>
<div class="home">
<div id="map-container" style="width:100%; height:500px"></div>
</div>
</template>
<script>
import AMap from "AMap";
export default {
name: 'Home',
data(){
return {
map:null
}
},
components: {
},
created(){
let that = this;
that.$nextTick(() => {
that.initMap();
});
},
methods:{
initMap(){
let that = this;
var buildings = new AMap.Buildings({
'zooms':[16,18],
'zIndex':10,
'heightFactor':2
});
that.map = new AMap.Map('map-container', {
center: [116.397428, 39.90923],
viewMode:'3D',
pitch:60,
rotation:-35,
features:['bg','road','point'],
mapStyle:'amap://styles/light',
layers: [new AMap.TileLayer.Satellite(),buildings],
zoom: 16
});
}
}
}
</script>
<style type="text/css">
#container {width:300px; height: 180px; }
</style>
|