Get started
Get started

Vue, Svelte and JavaScript quickstart

Vue, Svelte and JavaScript quickstart

MountayaMap is the map itself, with no framework behind it. Mount it in any element.

1

Create the app
npm create vite@latest my-map -- --template vanilla-ts
cd my-map
npm install @mountaya/elements @mountaya/client maplibre-gl@6.16.0

2

Set your key

Create a publishable key under Organization → API keys in the Mountaya app, with the tiles and publishing scopes.

.env.local
VITE_MOUNTAYA_PUBLISHABLE_KEY=pk_your_publishable_key
VITE_MOUNTAYA_ITINERARY_ID=0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21

3

Give the map a container

In index.html, replace the <div id="app"> with a container that has a height:

<div id="map" style="height: 100vh"></div>

4

Replace src/main.ts
src/main.ts
/// <reference types="vite/client" />
import { Mountaya } from "@mountaya/client/browser";
import { MountayaMap } from "@mountaya/elements";
import "@mountaya/elements/styles.css";

const client = new Mountaya({
  credential: { publishableKey: import.meta.env.VITE_MOUNTAYA_PUBLISHABLE_KEY },
});

const map = new MountayaMap({
  center: [6.8712, 45.9231],
  client,
  container: "map",
  zoom: 12,
});

map.on("accessrequired", ({ status, url }) => console.warn(`${status} on ${url}`));
map.on("error", ({ error }) => console.error(error));

// Draws the itinerary and frames the camera on it.
map.setItinerary(import.meta.env.VITE_MOUNTAYA_ITINERARY_ID, { fit: true }).catch(console.error);

Run npm run dev and open the address Vite prints.

In Vue or Svelte

Build the map when the component mounts, and destroy it when it unmounts.

<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from "vue";
import { Mountaya } from "@mountaya/client/browser";
import { MountayaMap } from "@mountaya/elements";
import "@mountaya/elements/styles.css";

const container = ref<HTMLDivElement | null>(null);
let map: MountayaMap | undefined;

onMounted(() => {
  if (container.value === null) return;
  const client = new Mountaya({
    credential: { publishableKey: import.meta.env.VITE_MOUNTAYA_PUBLISHABLE_KEY },
  });
  map = new MountayaMap({ center: [6.8712, 45.9231], client, container: container.value, zoom: 12 });
});

onBeforeUnmount(() => map?.destroy());
</script>

<template>
  <div ref="container" style="height: 480px" />
</template>

Every method of the map is on Use the map without React.

Next steps

Last updated