شروع سریع
نصب، واردکردن CSS و نمایش اولین نقشه با یک کامپوننت کامل.
پیشنیازها#
از Vue نسخهٔ 3.5.43 یا جدیدتر استفاده کنید. ابزارهای بیلد به Node نسخهٔ 22.12 به بالا در شاخهٔ ۲۲ یا نسخهٔ ۲۴ به بالا نیاز دارند. MapLibre به WebGL و DecompressionStream نیاز دارد. پیشنیاز نسخهٔ Node در Nuxt 4 سختگیرانهتر است؛ راهنمای اختصاصی Nuxt را بخوانید.
نصب#
npm install vue-neshan-mapsتنظیم کلید#
در پنل نشان یک کلید وب بسازید و دامنههای توسعه و محصول را مجاز کنید. در Vite مقدار را در فایل محیطی وارد کنید که در Git ثبت نمیشود:
VITE_NESHAN_WEB_KEY=web.YOUR_DOMAIN_ALLOWED_KEYنمونهٔ کامل Vue#
نمونهٔ زیر امکانات اصلی را کنار هم دارد: ترافیک و مکانها، مارکر قابل جابهجایی، محدودهٔ خدمات، تغییر سبک، انتخاب نقطه و کنترلهای برنامه. بسته، پنل یا نوار کناری آماده به برنامه تزریق نمیکند؛ رابط را مطابق نیازتان طراحی کنید.
کلید VITE_NESHAN_WEB_KEY را در .env.local بگذارید. پس از تغییر متغیر محیطی، Vite را دوباره اجرا کنید. متن دکمههای این نمونه را میتوانید فارسی کنید.
<script setup lang="ts">
import { ref } from 'vue'
import {
NeshanMapLibre, MAPLIBRE_STYLES,
type Coordinates, type MapLibreMarker, type MapCircle,
type MapPolygon, type MapPolyline, type TileState,
} from 'vue-neshan-maps'
import 'vue-neshan-maps/style.css'
const webKey = import.meta.env.VITE_NESHAN_WEB_KEY
const center = ref<Coordinates>([35.6997, 51.338])
const zoom = ref(13)
const traffic = ref(true)
const poi = ref(true)
const dark = ref(false)
const picking = ref(false)
const mapRef = ref<InstanceType<typeof NeshanMapLibre>>()
const initialized = ref(false)
const resourceStatus = ref('idle')
const message = ref('')
const markers = ref<MapLibreMarker[]>([
{ id: 'azadi', position: [35.6997, 51.338], popup: 'Azadi Square',
tooltip: 'Drag to edit', options: { color: '#087f8c', draggable: true } },
{ id: 'laleh', position: [35.711, 51.39], popup: 'Laleh Park' },
])
const circles: MapCircle[] = [
{ id: 'service', center: [35.711, 51.39], radius: 620,
options: { color: '#087f8c', fillColor: '#1eb5aa', fillOpacity: 0.15 } },
]
const polygons: MapPolygon[] = [
{ id: 'area', positions: [[35.721, 51.346], [35.728, 51.354], [35.722, 51.365]],
options: { color: '#b66a20', fillColor: '#e3ad58', fillOpacity: 0.2 } },
]
const polylines: MapPolyline[] = [
{ id: 'connection', positions: [[35.6997, 51.338], [35.711, 51.39]],
options: { color: '#087f8c', weight: 3, dashArray: '6,5' } },
]
function saveMarker(marker: MapLibreMarker, position: Coordinates) {
markers.value = markers.value.map(item => item.id === marker.id ? { ...item, position } : item)
}
function resources(state: TileState) { resourceStatus.value = state.status }
function showError(error: Error) { message.value = error.message }
function locationError(error: { code: number; message: string }) { message.value = error.message }
function locate() { mapRef.value?.locate({ setView: true, maxZoom: 16 }) }
function inspect() {
const report = mapRef.value?.getDiagnostics()
message.value = report
? `${report.markers} markers; ${report.trafficFeatures} traffic features; ${report.poiFeatures} POI features in this view`
: 'Map is not ready yet'
}
</script>
<template>
<section aria-label="Map controls">
<label><input v-model="traffic" type="checkbox">Traffic</label>
<label><input v-model="poi" type="checkbox">Places</label>
<label><input v-model="dark" type="checkbox">Dark style</label>
<label><input v-model="picking" type="checkbox">Pick a point</label>
<button :disabled="!initialized" @click="mapRef?.fitToMarkers()">Show all places</button>
<button :disabled="!initialized" @click="mapRef?.flyTo([35.711, 51.39], 15)">Laleh Park</button>
<button :disabled="!initialized" @click="mapRef?.openPopup('azadi')">Open Azadi</button>
<button :disabled="!initialized" @click="locate">My location</button>
<button :disabled="!initialized" @click="inspect">Inspect layers</button>
</section>
<NeshanMapLibre
ref="mapRef" v-model="center" v-model:zoom="zoom" :api-key="webKey"
:map-style="dark ? MAPLIBRE_STYLES.dark : MAPLIBRE_STYLES.light"
:traffic="traffic" :poi="poi" :pick-on-click="picking"
:center-marker="picking" :markers="markers"
:circles="circles" :polygons="polygons" :polylines="polylines"
:styles="{ height: '520px' }" aria-label="Tehran places and service area"
@ready="initialized = true" @destroy="initialized = false"
@marker-dragend="saveMarker" @tiles-loading="resources"
@tiles-loaded="resources" @tile-error="resources"
@warning="message = $event" @error="showError"
@location-error="locationError"
>
<template #loading><p role="status">Preparing the map…</p></template>
<template #error="{ error, retry }">
<p role="alert">{{ error.message }} <button @click="retry">Try again</button></p>
</template>
<template #tile-error="{ state }">
<p role="status">Some resources failed ({{ state.failed }}). Check the key and network.</p>
</template>
</NeshanMapLibre>
<p>Selected center: {{ center[0].toFixed(5) }}, {{ center[1].toFixed(5) }} · Zoom: {{ zoom }}</p>
<p role="status">Map resources: {{ resourceStatus }}. {{ message }}</p>
</template>بررسی نتیجه#
باید نقشه و انتساب آن را ببینید. نشانهٔ مرکز با روشنبودن centerMarker نمایش داده میشود. نبود CSS، ارتفاع صفر، خطای کلید یا دامنه و نبود WebGL از علتهای صفحهٔ خالیاند. دریافت رویداد ready بهتنهایی مجوز دریافت کاشی را اثبات نمیکند.