vue3-openlayers

repository·main·Indexed 21 days ago

https://github.com/melihaltintas/vue3-openlayers

A component library that integrates the OpenLayers API into the Vue 3 reactive ecosystem. It allows developers to build web maps using tiled, raster, or vector layers. The library includes an Animations plugin providing specialized components such as ol-animation-drop, ol-animation-fade, ol-animation-path, ol-animation-shake, ol-animation-slide, and ol-animation-teleport.

Tokens
54.7K
Snippets
216
Records
314
Agent score
73%

What's inside vue3-openlayers

  1. Overview of vue3-openlayers

    main

    vue3-openlayers

    vue3-openlayers is a component library that integrates the OpenLayers API into the Vue 3 reactive ecosystem. It enables the display of maps using various layer types, including tiled, raster, or vector layers loaded from different sources.

  2. Configure renderMode for ol-vector-tile-layer

    main

    The renderMode property controls how vector tiles are drawn on the map:

    • hybrid (Default): Polygon and line elements are rendered as images (pixels scale during zoom animations), while point symbols and text are rendered as vectors to remain accurate and upright during rotation.
    • vector: Everything is rendered as vectors. This mode is recommended for improved performance when the layer contains only a few features (e.g., for highlighting a subset of features from another layer).
  3. Use ol-style-stroke to style lines, polygons, circles, and text

    main

    The ol-style-stroke component is used to define the stroke properties for geometric shapes. It is designed to be used as a nested component within other styling components:

    • Inside ol-style to style lines and polygons.
    • Inside ol-style-circle to style circles.
    • Inside ol-style-text to style text.

    You can use it via the plugin component <ol-style-stroke> or by explicitly importing <Styles.OlStyleStroke>.

    <!-- Plugin Usage -->
    <ol-style-stroke color="red" :width="2" />
    
    <!-- Explicit Import Usage -->
    <Styles.OlStyleStroke color="#ff0000" :width="2" />
  4. Use the ol-feature component to containerize geometries

    main

    The ol-feature component acts as a container for a single geometry. To display features on a map, you typically use ol-feature in conjunction with ol-vector-layer and ol-source-vector to render GeoJSON features or manual geometries.

    Key constraints:

    • An ol-feature must contain exactly one child element representing a Geometry (e.g., ol-geom-point).
    • An ol-feature must include an ol-style component to define how the geometry is rendered.
    • To display multiple geometries, you must create and add multiple ol-feature components.
    <ol-feature>
      <ol-geom-point />
      <ol-style ... />
    </ol-feature>
  5. Use ol-vector-image-layer for high-performance vector rendering

    main

    The ol-vector-image-layer component renders vector data from various backend services by drawing them to an image client-side. This provides significantly better performance during panning and zooming compared to ol-vector-layer.

    Note on behavior: Because it renders to an image, point symbols and text will always rotate with the view, and pixels will be scaled during zoom animations.

    To use this component, you must pair it with an ol-source-vector component.

    <ol-vector-image-layer>
      <ol-source-vector ... />
    </ol-vector-image-layer>
  6. Optimize performance for large datasets in ol-animated-cluster-layer

    main

    When working with a large number of markers or clusters, passing features directly to ol-source-vector can cause performance issues, such as rendering errors, blocked interactive elements, or memory leaks.

    Recommendation: Instead of passing feature objects directly, request features from a URL (e.g., using a GeoJSON endpoint). This allows the source to manage data loading more efficiently.

    <template>
      <ol-map
        :loadTilesWhileAnimating="true"
        :loadTilesWhileInteracting="true"
        style="height: 400px"
      >
        <ol-animated-cluster-layer :animationDuration="500" :distance="40">
          <ol-source-vector :url="url" :format="geoJson" />
        </ol-animated-cluster-layer>
      </ol-map>
    </template>
    
    <script setup lang="ts">
    import { computed, inject } from "vue";
    
    // ...
    
    const format = inject("ol-format");
    const geoJson = new format.GeoJSON();
    const url = computed(() => {
      return `http://localhost:3000/${count.value}`;
    });
    </script>
  7. Performance considerations for large vector datasets

    main

    When dealing with large numbers of markers or features, consider the following:

    • URL vs. Direct Features: Requesting data via a url (GeoJSON) is generally more performant than passing an array of features via the features property.
    • Data Transfer & Streaming: Using a url allows the browser to stream GeoJSON in chunks and fetch data as needed, reducing initial load time.
    • Memory Management: Passing features directly loads the entire dataset into memory at once, which can lead to high memory usage or crashes. Using a url allows OpenLayers to manage data on-demand.
    • Server-side Optimization: Using a url enables server-side filtering and caching, which reduces network load and improves response times.
  8. Use the ol-style component for custom vector styling

    main

    The ol-style component acts as a container for a collection of styles. It is used to provide custom visual representations to vector elements. You can nest <ol-style> inside several components to apply styles to specific parts of the map:

    • ol-feature: To style an individual feature.
    • ol-vector-layer: To style all features within a specific layer.
    • ol-interaction-select: To style features during a selection interaction.
    • ol-interaction-draw: To style features being drawn.
    • ol-interaction-modify: To style features during a modification interaction.
    <ol-style>
      <!-- Style definitions go here -->
    </ol-style>
  9. Use explicit component imports for Map

    main

    If you prefer not to install the plugin globally, you can use explicit component imports. When using explicit imports, the Map component is available as a child of the named export Map.

    Important: If you choose this method, you must adjust your template component names. For example, instead of using the standard <ol-map>, you must use <Map.OlMap>.

  10. Use the ol-style-text component

    main

    The ol-style-text component is used to style the text of a shape within an OpenLayers map. You can use it either via the plugin (as a component tag) or via explicit import from the Styles object.

    Plugin Usage: <ol-style-text>

    Explicit Import: <Styles.OlStyleText>

    <template>
      <!-- Plugin usage -->
      <ol-style-text :fill="'red'" :stroke="'black'" />
    
      <!-- Explicit import usage -->
      <Styles.OlStyleText :fill="'blue'" />
    </template>
    
    <script setup>
    import { Styles } from 'vue3-openlayers'
    </script>