Building a Map Application with MapLibre GL JS and Vue.js (script setup)
Tags: maplibre, vue, vite, foss4g
Published: 2023-09-18 10:02:02

![img](https://memo.dayjournal.dev/images/try-101_02.png)

<br>

### I built a development environment combining MapLibre GL JS and Vue.js (script setup) :tada:  

<br>

The created environment is available on GitHub. Please use it! 

{% embed https://github.com/mug-jp/maplibregljs-vue-starter %}

<br>

## Advance Preparation

- Creating a Vue.js project

[Vue.js #006 - environment setup with create-vue](https://memo.dayjournal.dev/memo/vue-js-006/)

![img](https://memo.dayjournal.dev/images/try-101_03.png)

Execution environment

- node v20.6.1
- npm v9.8.1

<br>

## Install MapLibre GL JS
Install MapLibre GL JS into your Vue.js project.

```sh
npm install maplibre-gl
```

![img](https://memo.dayjournal.dev/images/try-101_04.png)

<br>

## Building the map application
Finally, let's build the actual map application. Change or delete some files from the template.  

Overall composition
![img](https://memo.dayjournal.dev/images/try-101_05.png)

<br>

#### package.json

```json
{
  "name": "maplibregljs-vue-starter",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "dev": "vite",
    "build": "run-p type-check \"build-only {@}\" --",
    "preview": "vite preview",
    "test:unit": "vitest",
    "test:e2e": "start-server-and-test preview http://localhost:4173 'cypress run --e2e'",
    "test:e2e:dev": "start-server-and-test 'vite dev --port 4173' http://localhost:4173 'cypress open --e2e'",
    "build-only": "vite build",
    "type-check": "vue-tsc --noEmit -p tsconfig.vitest.json --composite false",
    "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
    "format": "prettier --write src/"
  },
  "keywords": [],
  "author": "MapLibre User Group Japan",
  "license": "ISC",
  "dependencies": {
    "maplibre-gl": "^3.3.1",
    "pinia": "^2.1.6",
    "vue": "^3.3.4",
    "vue-router": "^4.2.4"
  },
  "devDependencies": {
    "@rushstack/eslint-patch": "^1.3.3",
    "@tsconfig/node18": "^18.2.2",
    "@types/jsdom": "^21.1.2",
    "@types/node": "^18.17.15",
    "@vitejs/plugin-vue": "^4.3.4",
    "@vue/eslint-config-prettier": "^8.0.0",
    "@vue/eslint-config-typescript": "^12.0.0",
    "@vue/test-utils": "^2.4.1",
    "@vue/tsconfig": "^0.4.0",
    "cypress": "^13.2.0",
    "eslint": "^8.49.0",
    "eslint-plugin-cypress": "^2.14.0",
    "eslint-plugin-vue": "^9.17.0",
    "jsdom": "^22.1.0",
    "npm-run-all": "^4.1.5",
    "prettier": "^3.0.3",
    "start-server-and-test": "^2.0.0",
    "typescript": "~5.1.6",
    "vite": "^4.4.9",
    "vitest": "^0.34.4",
    "vue-tsc": "^1.8.11"
  }
}
```

<br>

/src
#### App.vue
Remove the existing page and use only the router.

```vue
<script setup lang="ts">
import { RouterView } from 'vue-router'
</script>

<template>
  <RouterView />
</template>

<style scoped></style>
```

<br>

/src/assets
#### main.css
Reset existing CSS in App.vue.

```css
@import './base.css';

html,
body,
#app {
  color: black;
}

a,
.green {
  text-decoration: none;
  color: hsla(160, 100%, 37%, 1);
  transition: 0.4s;
}

@media (hover: hover) {
  a:hover {
    background-color: hsla(160, 100%, 37%, 0.2);
  }
}
```

<br>

/src/router
#### index.ts
Change the existing router to HomeView only.

```typescript
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    }
  ]
})

export default router
```

<br>

/src/views
#### HomeView.vue
Change to load the map component.

```vue
<script setup lang="ts">
import MapPane from '../components/MapPane.vue'
</script>

<template>
  <main>
    <MapPane />
  </main>
</template>
```

<br>

/src/components
#### MapPane.vue
Create a new MapLibre GL JS map component.

```vue
<script setup lang="ts">
import 'maplibre-gl/dist/maplibre-gl.css'
import { Map, NavigationControl } from 'maplibre-gl'
import { onMounted } from 'vue'

onMounted(() => {
  const map = new Map({
    container: 'map',
    style: {
      version: 8,
      sources: {
        MIERUNEMAP: {
          type: 'raster',
          tiles: ['https://tile.mierune.co.jp/mierune_mono/{z}/{x}/{y}.png'],
          tileSize: 256,
          attribution:
            "Maptiles by <a href='http://mierune.co.jp/' target='_blank'>MIERUNE</a>, under CC BY. Data by <a href='http://osm.org/copyright' target='_blank'>OpenStreetMap</a> contributors, under ODbL."
        }
      },
      layers: [
        {
          id: 'MIERUNEMAP',
          type: 'raster',
          source: 'MIERUNEMAP',
          minzoom: 0,
          maxzoom: 18
        }
      ]
    },
    center: [139.767, 35.681],
    zoom: 11
  })

  map.addControl(
    new NavigationControl({
      visualizePitch: true
    })
  )
})
</script>

<template>
  <div id="map"></div>
</template>

<style scoped>
#map {
  height: 100vh;
}
</style>
```

<br>

Let's check with a simple local server.

```sh
npm run serve
```

<br>

You can display it using a combination of Vue.js (script setup) and MapLibre GL JS!
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zug0ww13j0m54gszsotk.png)

<br>

Related Articles
{% link https://dev.to/mug-jp/building-a-map-application-with-maplibre-gl-js-and-svelte-pg6 %}
{% link https://dev.to/aws-heroes/vite-support-for-various-map-library-starters-4m82 %}
{% link https://dev.to/mierune/comtiles-cloud-optimized-map-tiles-hosted-on-amazon-s3-and-visualized-with-maplibre-gl-js-3bed %}
{% link https://dev.to/mierune/trying-out-various-turfjs-547k %}
{% link https://dev.to/aws-heroes/amazon-location-service-and-aws-amplify-to-use-various-map-library-4mmk %}

<br>

References
[MapLibre GL JS](https://maplibre.org/)
[Vue.js](https://jp.vuejs.org)