vue 引入 maptalks基础
npm install maptalks
<template lang="pug">
#map.container
</template>
<script>
import "maptalks/dist/maptalks.css";
import * as maptalks from "maptalks";
export default {
data() {
return {
map: "",
};
},
created() {},
mounted() {
this.$nextTick(() => {
this.map = new maptalks.Map("map", {
center: [120.20377025390627, 30.19386063195693],
zoom: 14,
dragPitch: true,
dragRotate: true,
dragRotatePitch: false,
baseLayer: new maptalks.TileLayer("base", {
urlTemplate:
"http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png",
subdomains: ["a", "b", "c", "d"],
attribution:
'© <a href="http://osm.org">OpenStreetMap</a> contributors, © <a href="https://carto.com/">CARTO</a>',
}),
layers: [new maptalks.VectorLayer("v")],
});
var toolbar = new maptalks.control.Toolbar({
items: [
{
item: "↑",
click: this.up,
},
{
item: "↓",
click: this.down,
},
{
item: "←",
click: this.left,
},
{
item: "→",
click: this.right,
},
{
item: "pan to",
click: this.toCoordinate,
},
],
}).addTo(this.map);
console.log("map: ", this.map);
});
},
methods: {
up() {
this.map.panBy([0, -200]);
},
down() {
this.map.panBy([0, 200]);
},
left() {
this.map.panBy([-200, 0]);
},
right() {
this.map.panBy([200, 0]);
},
toCoordinate() {
var symbol = {
markerType: "x",
markerLineColor: "#f00",
markerLineWidth: 4,
markerWidth: 20,
markerHeight: 20,
};
var coordinate = this.map.getCenter().add(0.008, 0.008);
this.map
.getLayer("v")
.clear()
.addGeometry(new maptalks.Marker(coordinate, { symbol: symbol }));
this.map.panTo(coordinate);
},
},
};
</script>
<style lang="scss" scoped>
.container {
width: 100%;
height: 100%;
}
</style>