پرش به محتوای اصلی
Vue Neshan Mapsراهنمای توسعه‌دهندگان

شروع سریع

نصب، واردکردن CSS و نمایش اولین نقشه با یک کامپوننت کامل.

۴ دقیقه مطالعهVue 3 / Nuxt 4همراه مثال زنده
مطالب این صفحهپیش‌نیازهانصبتنظیم کلیدنمونهٔ کامل Vueبررسی نتیجه
مثال تعاملیMapLibre

اجرا روی نقشهٔ واقعی نشان

کلید وب با مجوز دامنه وارد کنید. هندسه نمونه است؛ نقشهٔ پایه واقعی است.

دامنهٔ sajadjanat.github.io را مجاز کنید. کلید فقط در حافظه می‌ماند و وارد کد کپی‌شده نمی‌شود.

پیش‌نیازها#

از Vue نسخهٔ 3.5.43 یا جدیدتر استفاده کنید. ابزارهای بیلد به Node نسخهٔ 22.12 به بالا در شاخهٔ ۲۲ یا نسخهٔ ۲۴ به بالا نیاز دارند. MapLibre به WebGL و DecompressionStream نیاز دارد. پیش‌نیاز نسخهٔ Node در Nuxt 4 سخت‌گیرانه‌تر است؛ راهنمای اختصاصی Nuxt را بخوانید.

نصب#

bash
npm install vue-neshan-maps

تنظیم کلید#

در پنل نشان یک کلید وب بسازید و دامنه‌های توسعه و محصول را مجاز کنید. در Vite مقدار را در فایل محیطی وارد کنید که در Git ثبت نمی‌شود:

dotenv
VITE_NESHAN_WEB_KEY=web.YOUR_DOMAIN_ALLOWED_KEY

نمونهٔ کامل Vue#

‏نمونهٔ زیر امکانات اصلی را کنار هم دارد: ترافیک و مکان‌ها، مارکر قابل جابه‌جایی، محدودهٔ خدمات، تغییر سبک، انتخاب نقطه و کنترل‌های برنامه. بسته، پنل یا نوار کناری آماده به برنامه تزریق نمی‌کند؛ رابط را مطابق نیازتان طراحی کنید.

‏کلید VITE_NESHAN_WEB_KEY را در .env.local بگذارید. پس از تغییر متغیر محیطی، Vite را دوباره اجرا کنید. متن دکمه‌های این نمونه را می‌توانید فارسی کنید.

vue
<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 به‌تنهایی مجوز دریافت کاشی را اثبات نمی‌کند.

کتابخانهٔ مستقل جامعهٔ توسعه‌دهندگان؛ سرویس و انتساب نقشه متعلق به نشان و OpenStreetMap است. · Sajad Janat · MIT