<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 时空路由器</title>
    <description>The latest articles on DEV Community by 时空路由器 (@chinesejar).</description>
    <link>https://dev.to/chinesejar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F184323%2F8d27c543-2b49-442b-ae9e-c63428970cd1.png</url>
      <title>DEV Community: 时空路由器</title>
      <link>https://dev.to/chinesejar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chinesejar"/>
    <language>en</language>
    <item>
      <title>How to use MapboxGL in Vue more elegantly</title>
      <dc:creator>时空路由器</dc:creator>
      <pubDate>Fri, 26 Aug 2022 04:00:00 +0000</pubDate>
      <link>https://dev.to/chinesejar/how-to-use-mapboxgl-in-vue-more-elegantly-15ec</link>
      <guid>https://dev.to/chinesejar/how-to-use-mapboxgl-in-vue-more-elegantly-15ec</guid>
      <description>&lt;p&gt;Vue provides a declarative and component-based programming model that helps you efficiently develop user interfaces, be it simple or complex.&lt;/p&gt;

&lt;p&gt;To more elegantly in using mapbox-gl, there is a package named &lt;a href="https://github.com/timeroute/mapvue"&gt;&lt;code&gt;MapVue&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;MapVue is a comprehensive MapboxGL component library. You can easily and happily apply MapVue to your Vue projects. Import various data sources and layers in the form of components, and update props of layers and sources in map by MVVM.&lt;/p&gt;

&lt;h3&gt;
  
  
  How it works?
&lt;/h3&gt;

&lt;p&gt;MapVue essentially wraps some classes in MapboxGL and implements componentization through some variable properties of the watch class.&lt;/p&gt;

&lt;p&gt;For example, the &lt;code&gt;v-fill-layer&lt;/code&gt; component actually wraps the &lt;code&gt;FillLayer&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;There are &lt;a href="https://mapvue.netlify.app/components/#common-components"&gt;&lt;code&gt;25 components&lt;/code&gt;&lt;/a&gt; im MapVue, almost contains all of the mapbox-gl APIS.&lt;/p&gt;

&lt;p&gt;The core component is &lt;code&gt;VMap&lt;/code&gt; component. It is the top-level component, all other components must be wrapped in &lt;code&gt;VMap&lt;/code&gt;, it instantiates a &lt;code&gt;mapboxgl.Map&lt;/code&gt; and provides the &lt;code&gt;map&lt;/code&gt; instance to child components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Components lists
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Core Components

&lt;ul&gt;
&lt;li&gt;VMap&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Common Components

&lt;ul&gt;
&lt;li&gt;VMarker: wrap &lt;code&gt;mapboxgl.Marker&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;VPopup: wrap &lt;code&gt;mapboxgl.Popup&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;VSprit: add an image to the style&lt;/li&gt;
&lt;li&gt;VFog: wrap &lt;code&gt;map.setFog&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;VFeatureState: set the state of a feature&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Control Components

&lt;ul&gt;
&lt;li&gt;VAttributionControl: wrap &lt;code&gt;AttributionControl&lt;/code&gt; control&lt;/li&gt;
&lt;li&gt;VNavigationControl: wrap &lt;code&gt;NavigationControl&lt;/code&gt; control&lt;/li&gt;
&lt;li&gt;VScaleControl: wrap &lt;code&gt;ScaleControl&lt;/code&gt; control&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Layer Components

&lt;ul&gt;
&lt;li&gt;VBackgroundLayer: wrap &lt;code&gt;background&lt;/code&gt; layer&lt;/li&gt;
&lt;li&gt;VCircleLayer: wrap &lt;code&gt;circle&lt;/code&gt; layer&lt;/li&gt;
&lt;li&gt;VFillExtrusionLayer: wrap &lt;code&gt;fillextrusion&lt;/code&gt; layer&lt;/li&gt;
&lt;li&gt;VFillLayer: wrap &lt;code&gt;fill&lt;/code&gt; layer&lt;/li&gt;
&lt;li&gt;VHeatmapLayer: wrap &lt;code&gt;heatmap&lt;/code&gt; layer&lt;/li&gt;
&lt;li&gt;VHillshadeLayer: wrap &lt;code&gt;hillshade&lt;/code&gt; layer&lt;/li&gt;
&lt;li&gt;VLineLayer: wrap &lt;code&gt;line&lt;/code&gt; layer&lt;/li&gt;
&lt;li&gt;VRasterLayer: wrap &lt;code&gt;raster&lt;/code&gt; layer&lt;/li&gt;
&lt;li&gt;VSymbolLayer: wrap &lt;code&gt;symbol&lt;/code&gt; layer&lt;/li&gt;
&lt;li&gt;VCanvasLayer: wrap &lt;code&gt;canvas&lt;/code&gt; layer&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Source Components

&lt;ul&gt;
&lt;li&gt;VGeoSource: wrap &lt;code&gt;geojson&lt;/code&gt; source&lt;/li&gt;
&lt;li&gt;VVectorSource: wrap &lt;code&gt;vector&lt;/code&gt; source&lt;/li&gt;
&lt;li&gt;VImageSource: wrap &lt;code&gt;image&lt;/code&gt; source&lt;/li&gt;
&lt;li&gt;VVideoSource: wrap &lt;code&gt;video&lt;/code&gt; source&lt;/li&gt;
&lt;li&gt;VRasterSource: wrap &lt;code&gt;raster&lt;/code&gt; source&lt;/li&gt;
&lt;li&gt;VRasterDemSource: wrap &lt;code&gt;rasterdem&lt;/code&gt; source&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Install
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# use yarn&lt;/span&gt;
yarn add mapvue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# use pnpm&lt;/span&gt;
pnpm add mapvue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Import
&lt;/h3&gt;

&lt;p&gt;Vite&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createApp } from "vue";
import MapVue from "mapvue";
import "mapvue/dist/mapvue.css";
import App from "./App.vue";

createApp(App).use(MapVue).mount("#app");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use case
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
import { defineComponent, reactive, watch } from "vue";
import { accessToken } from "../store";

export default defineComponent({
  setup() {
    const state = reactive({
      "heatmap-color": [
        "interpolate",
        ["linear"],
        ["heatmap-density"],
        0,
        "rgba(33,102,172,0)",
        0.2,
        "rgb(103,169,207)",
        0.4,
        "rgb(209,229,240)",
        0.6,
        "rgb(253,219,199)",
        0.8,
        "rgb(239,138,98)",
        1,
        "rgb(178,24,43)",
      ],
      "heatmap-opacity": ["interpolate", ["linear"], ["zoom"], 7, 1, 9, 0],
      "heatmap-radius": ["interpolate", ["linear"], ["zoom"], 0, 2, 9, 20],
      "heatmap-intensity": ["interpolate", ["linear"], ["zoom"], 0, 1, 9, 3],
      radius: 20,
      intensity: 3,
      layout: {
        visibility: "visible",
      },
    });

    return {
      state,
      accessToken,
    };
  },
});
&amp;lt;/script&amp;gt;

&amp;lt;template&amp;gt;
  &amp;lt;div class="container"&amp;gt;
    &amp;lt;v-map
      :accessToken="accessToken"
      :options="{
        center: [-124.137343, 45.137451],
        zoom: 3,
      }"
    &amp;gt;
      &amp;lt;v-geo-source
        id="geo"
        data="https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson"
      /&amp;gt;
      &amp;lt;v-heatmap-layer
        id="heatmap"
        source="geo"
        :paint="{
          'heatmap-color': state['heatmap-color'],
          'heatmap-opacity': state['heatmap-opacity'],
          'heatmap-radius': state['heatmap-radius'],
          'heatmap-intensity': state['heatmap-intensity'],
        }"
        :layout="state.layout"
      /&amp;gt;
    &amp;lt;/v-map&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;style scoped&amp;gt;
.container {
  height: 100vh;
  width: 100%;
}
&amp;lt;/style&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ztvt5i1B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xcgi8wrw5ib7ig1keh6q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ztvt5i1B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xcgi8wrw5ib7ig1keh6q.png" alt="render heatmap" width="880" height="684"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;more examples please visiting &lt;a href="https://codesandbox.io/s/vmap-examples-mnqjgn"&gt;examples&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github: &lt;a href="https://github.com/timeroute/mapvue"&gt;MapVue&lt;/a&gt;&lt;br&gt;
docs: &lt;a href="https://mapvue.netlify.app/"&gt;MapVue doc&lt;/a&gt;&lt;/p&gt;

</description>
      <category>vue</category>
      <category>gis</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
