ghettovoice / vuelayers

Web map Vue components with the power of OpenLayers
https://vuelayers.github.io/
MIT License
683 stars 229 forks source link

Access openlayers events #303

Closed RemiDesgrange closed 4 years ago

RemiDesgrange commented 4 years ago

My goals is to fit the extent of the vector source. Since this source is updated, I thought of using the addFeature event emitted by VectorSource to fit on the extend. How can I do this ? And how to subscribe to ol events in my vue app ? I didn't found anything in the code about event, I search in mixins.

Something like this

<template>
            <vl-map :load-tiles-while-animating="true" :load-tiles-while-interacting="true"
                    data-projection="EPSG:4326" style="height: 400px" ref="map" @postcompose="onMapPostCompose">
                <vl-view :zoom="zoom" :center.sync="center" :rotation="rotation" ref="view"></vl-view>
                <vl-layer-tile id="osm">
                    <vl-source-osm></vl-source-osm>
                </vl-layer-tile>

                <vl-layer-vector render-mode="image">
                    <vl-source-vector ref="vectorsource" :features="getFeatures" @addfeature="featureAdded"></vl-source-vector>
                </vl-layer-vector>
            </vl-map>
</template>

<script>
    import Vue from 'vue'
    import VueLayers from 'vuelayers'

    import 'vuelayers/lib/style.css'
    import {mapGetters} from "vuex"

    Vue.use(VueLayers, {dataProjection: 'EPSG:4326'})

    export default {
      data() {
        return {
          center: [-40, 0],
          rotation: 0,
          zoom: 4
          //style: new Style()
        }
      },
      computed: {
        mapGetters(['getFeatures']),
      },
      methods: {
        onMapPostCompose() {
          try {
            //this.$refs.view.$view.fit(this.$refs.map.$map.getLayers().getArray().find(e => e.type === 'VECTOR').getSource().getExtent())
          } catch (e) {
            // ignore
          }
        },
        featureAdded() {
              this.$refs.view.$view.fit(this.$refs.map.$map.getLayers().getArray().find(e => e.type === 'VECTOR').getSource().getExtent())
        }
      }
      }
    }
</script>
ghettovoice commented 4 years ago

Hello @RemiDesgrange ! In the v0.11.x version (that is current stable) this events named a bit differently. Use add:feature / remove:feature events.

This my fault, in the next release I plan to make event naming similar to openlayers events.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.