要將leaflet集成到Vue項目中,首先需要安裝Leaflet和Vue2-leaflet插件。
npm install leaflet vue2-leaflet
import { LMap, LTileLayer, LMarker } from 'vue2-leaflet';
import 'leaflet/dist/leaflet.css';
export default {
components: {
LMap,
LTileLayer,
LMarker,
}
}
<template>
<l-map
style="height: 400px;"
:zoom="zoom"
:center="center"
>
<l-tile-layer
:url="url"
></l-tile-layer>
<l-marker
:lat-lng="marker"
></l-marker>
</l-map>
</template>
data() {
return {
zoom: 13,
center: [37.7749, -122.4194],
url: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
marker: [37.7749, -122.4194],
};
}
通過以上步驟,您可以將Leaflet集成到Vue項目中,并在Vue組件中使用Leaflet地圖組件。