在本教程中,您將學(xué)習(xí)如何創(chuàng)建一個(gè) Vue.js 組件來(lái)使用 MapLibre GL JS 渲染地圖。我們將一起制作一個(gè)簡(jiǎn)單的全屏地圖應(yīng)用程序,作為如何將MapTiler地圖與Vue.js和MapLibre GL .js JS一起使用的示例。 在本教程結(jié)束時(shí),您將能夠在指定位置創(chuàng)建帶有標(biāo)記的全屏地圖。您的最終地圖將如下所示: 開(kāi)始完成本教程的最低要求。
npm install -g @vue/cli 砰砰?? 復(fù)制 創(chuàng)建應(yīng)用在此步驟中,我們將學(xué)習(xí)如何創(chuàng)建 Vue.js 應(yīng)用程序。 要?jiǎng)?chuàng)建一個(gè)新的 Vue.js 項(xiàng)目,請(qǐng)?jiān)诿钚兄羞\(yùn)行: vue create my-vue-map 砰砰?? 復(fù)制 該命令會(huì)提示您輸入有關(guān)要包含在初始應(yīng)用程序中的功能的信息。選擇該選項(xiàng)。 使用箭頭鍵并按 Enter 或 Return 鍵選擇一個(gè)選項(xiàng)。Vue CLI 安裝必要的 Vue.js npm 包和其他依賴項(xiàng),并創(chuàng)建一個(gè)新的工作區(qū)和一個(gè)簡(jiǎn)單的歡迎應(yīng)用程序,準(zhǔn)備運(yùn)行。有關(guān)更多信息,請(qǐng)參閱創(chuàng)建項(xiàng)目。 導(dǎo)航到新創(chuàng)建的項(xiàng)目文件夾 cd my-vue-map 砰砰?? 復(fù)制 在新創(chuàng)建的項(xiàng)目文件夾中,運(yùn)行以啟動(dòng)本地環(huán)境。您會(huì)發(fā)現(xiàn)您的應(yīng)用程序在地址上運(yùn)行。 現(xiàn)在,您應(yīng)該在瀏覽器中看到該應(yīng)用程序。 安裝和設(shè)置要安裝 MapLibre GL 庫(kù),請(qǐng)導(dǎo)航到您的項(xiàng)目文件夾并運(yùn)行以下命令: npm i maplibre-gl 砰砰?? 復(fù)制 導(dǎo)航到該文件夾并刪除文件的所有內(nèi)容。在文件中寫(xiě)入以下行 <template> <div class="app"> This is my map App </div></template><script>export default { name: 'App', components: { }}</script><style>body { margin: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;}code { font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace;}.app { text-align: center;}</style> .HTML 復(fù)制 現(xiàn)在,您應(yīng)該在瀏覽器中看到“這是我的地圖應(yīng)用程序”。 導(dǎo)航到文件夾并刪除 de 文件 創(chuàng)建導(dǎo)航欄組件在此步驟中,我們將創(chuàng)建一個(gè)簡(jiǎn)單的標(biāo)題導(dǎo)航欄組件。 創(chuàng)建一個(gè)在文件夾內(nèi)調(diào)用的新文件并編寫(xiě)以下行: <template> <div class="heading"> <h1>This is my map App</h1> </div></template><script>export default { name: 'Navbar'}</script><style scoped>.heading { margin: 0; padding: 0px; background-color: black; color: white;}.heading > h1 { padding: 20px; margin: 0;}</style> .HTML 復(fù)制 Finally, to display the we need to import the Navbar component and add it to our main component template section . Import the navbar component into script block <script> import Navbar from './components/Navbar.vue'; export default { name: 'App', components: { Navbar } }</script> HTML Copy Replace the text This is my map Appwith . Your file should look like this: <template> <div class="app"> <Navbar /> </div></template><script>import Navbar from './components/Navbar.vue';export default { name: 'App', components: { Navbar }}</script><style>body { margin: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;}code { font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace;}.app { text-align: center;}</style> HTML Copy Now you should see the black navbar at the top of your browser. Create a map componentIn this step, we will create a simple map component. Create a new file called inside the folder and write these lines: <template> <div class="map-wrap"> <a href="https://www." class="watermark"><img src="https://api./resources/logo.svg" alt="MapTiler logo"/></a> <div class="map" ref="mapContainer"></div> </div></template><script>import { Map } from 'maplibre-gl';import { shallowRef, onMounted, onUnmounted, markRaw } from 'vue';export default { name: "Map", setup () { const mapContainer = shallowRef(null); const map = shallowRef(null); onMounted(() => { const apiKey = 'YOUR_MAPTILER_API_KEY_HERE'; const initialState = { lng: 139.753, lat: 35.6844, zoom: 14 }; map.value = markRaw(new Map({ container: mapContainer.value, style: `https://api./maps/streets-v2/style.json?key=${apiKey}`, center: [initialState.lng, initialState.lat], zoom: initialState.zoom })); }), onUnmounted(() => { map.value?.remove(); }) return { map, mapContainer }; }};</script><style scoped>@import '~maplibre-gl/dist/maplibre-gl.css';.map-wrap { position: relative; width: 100%; height: calc(100vh - 77px); /* calculate height of the screen minus the heading */}.map { position: absolute; width: 100%; height: 100%;}.watermark { position: absolute; left: 10px; bottom: 10px; z-index: 999;}</style> HTML Copy We use on the map itself and on the wrap around the map for more possibilities in future styling. Here you will need to replace with your actual MapTiler API key.
Render a mapFinally, to display the we need to import the Map component and add it to our main component . Import the map component into script block <script>import Navbar from './components/Navbar.vue';import Map from './components/Map.vue';export default { name: 'App', components: { Navbar, Map }}</script> HTML Copy Add the just below the Navbar in the template section. The template block should look like this <template> <div class="app"> <Navbar /> <Map /> </div></template> HTML Copy With everything done up until now, you should be able see your beautiful map in your browser. Your file should look like this: <template> <div class="app"> <Navbar /> <Map /> </div></template><script>import Navbar from './components/Navbar.vue';import Map from './components/Map.vue';export default { name: 'App', components: { Navbar, Map }}</script><style>body { margin: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;}code { font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace;}.app { text-align: center;}</style> HTML Copy Basic additional optionsThe last topic of this tutorial will be adding basic objects to your map. For more detailed information you can visit the MapLibre documentation. Map ControlsWe will navigate back to our file and add map navigation controls to our map. Add the next to the Map object import from MapLibre GL. import { Map, NavigationControl } from 'maplibre-gl'; JavaScript Copy On line 30 (just after the initialization of the map) of the file, add the following line: map.value.addControl(new NavigationControl(), 'top-right'); JavaScript Copy
Map markerAnother basic thing to add to your map could be a marker of some location. Add the next to the Map object import from MapLibre GL. import { Map, NavigationControl, Marker } from 'maplibre-gl'; JavaScript Copy In the following line where we declare the navigation control, we add these lines: new Marker({color: "#FF0000"}) .setLngLat([139.7525,35.6846]) .addTo(map); JavaScript Copy We create a new marker using the function. We added the color option to make it red, then set Lng/Lat of the marker using function, and finally added it to the current map using function. We are finished with our basic map objects, your file should look like this: <template> <div class="map-wrap"> <a href="https://www." class="watermark"><img src="https://api./resources/logo.svg" alt="MapTiler logo"/></a> <div class="map" ref="mapContainer"></div> </div></template><script>import { Map, NavigationControl, Marker } from 'maplibre-gl';import { shallowRef, onMounted, onUnmounted, markRaw } from 'vue';export default { name: "Map", setup () { const mapContainer = shallowRef(null); const map = shallowRef(null); onMounted(() => { const apiKey = 'YOUR_MAPTILER_API_KEY_HERE'; const initialState = { lng: 139.753, lat: 35.6844, zoom: 14 }; map.value = markRaw(new Map({ container: mapContainer.value, style: `https://api./maps/streets-v2/style.json?key=${apiKey}`, center: [initialState.lng, initialState.lat], zoom: initialState.zoom })); map.value.addControl(new NavigationControl(), 'top-right'); new Marker({color: "#FF0000"}) .setLngLat([139.7525,35.6846]) .addTo(map.value); }), onUnmounted(() => { map.value?.remove(); }) return { map, mapContainer }; }};</script><style scoped>@import '~maplibre-gl/dist/maplibre-gl.css';.map-wrap { position: relative; width: 100%; height: calc(100vh - 77px); /* calculate height of the screen minus the heading */}.map { position: absolute; width: 100%; height: 100%;}.watermark { position: absolute; left: 10px; bottom: 10px; z-index: 999;}</style> HTML Copy 要下載的完整代碼我們利用本教程的結(jié)果創(chuàng)建了一個(gè)模板,該模板將作為構(gòu)建未來(lái)應(yīng)用程序的基礎(chǔ)。您可以在 Vue.js 的 MapLibre 模板中訪問(wèn)模板存儲(chǔ)庫(kù)。 在線演示:您可以在 https://labs./vue-template-maplibre-gl-js/ 結(jié)論祝賀!您已經(jīng)使用 Vue.js 完成了簡(jiǎn)單的全屏地圖應(yīng)用程序,在東京皇宮上用標(biāo)記顯示東京。您可以在 MapLibre API 參考中為您的地圖探索有關(guān) MapLibre GL JS 的更多信息。 有用的鏈接MapTiler - JavaScript Maps API Vue.js NPM - MapLibre GL MapLibre official web MapTiler Cloud - Customize |
|