Coder Social home page Coder Social logo

map.fly to about vue-mapbox-gl HOT 5 CLOSED

phegman avatar phegman commented on May 31, 2024
map.fly to

from vue-mapbox-gl.

Comments (5)

phegman avatar phegman commented on May 31, 2024

You can get the map object on load, save it to the data object and then use the flyTo method. Check out the below example of a .vue file, it should give you a starting point. It is based off of this example https://www.mapbox.com/mapbox-gl-js/example/flyto/

<template>
  <div id="app">
    <mapbox 
    access-token="your access token"
    :map-options="{
      container: 'map',
      style: 'mapbox://styles/mapbox/streets-v9',
      center: [-74.50, 40],
      zoom: 9
    }"
    @map-load="mapLoaded"
    >
    </mapbox>

    <button class="fly" @click="flyTo">Fly To</button>
  </div>
</template>

<script>
import Mapbox from 'mapbox-gl-vue';
export default {
  name: 'app',
  components: {
      'mapbox': Mapbox
  },
  data () {
    return {
      map: {}
    }
  },
  methods: {
    mapLoaded(map) {
      this.map = map;
    },
    flyTo() {
      this.map.flyTo({
        center: [
            -74.50 + (Math.random() - 0.5) * 10,
            40 + (Math.random() - 0.5) * 10]
      });
    }
  }
}
</script>

<style lang="scss">
  #map {
    width: 100%;
    height: 500px;
  }
</style>

from vue-mapbox-gl.

Durban-Designer avatar Durban-Designer commented on May 31, 2024

Thanks so much! You are a lifesaver.

from vue-mapbox-gl.

felimartina avatar felimartina commented on May 31, 2024

This doesn't seem to be working for me. Map breaks as soon as I zoom in or out. Problem goes away if I remove the line

this.map = map

any idea what might be going on?

from vue-mapbox-gl.

felimartina avatar felimartina commented on May 31, 2024

I was able to work around this by storing a reference to the map outside of vue 'data' object (aka global variable)

Code looks like this:

<template>
  <div>
    <mapbox
    @map-load="onMapLoaded"
    :accessToken="mapboxToken"
    :map-options="mapOptions"></mapbox>
    <!-- Can't bind options to a Vue DataObject because it breaks mapbox -->
  </div>
</template>

<script>
import Mapbox from "mapbox-gl-vue";
import Constants from "@/services/constants";
import Geolocation from "@/services/geolocation";

/**
 *  We have to keep the map reference outside vue 'data' object
 *  otherwise the mapbox styles break 
 */
let mapRef = {} // <--- HERE
export default {
  name: "Map",
  components: {
    Mapbox
  },
  data: function() {
    return {
      mapboxToken: process.env.VUE_APP_MAPBOX_TOKEN,
      mapOptions: {
        container: 'map',
        style: Constants.Map.Defaults.Style, //'mapbox://styles/mapbox/streets-v9',
        center: [
          Constants.Map.Defaults.Longitude,
          Constants.Map.Defaults.Latitude
        ],
        zoom: Constants.Map.Defaults.Zoom
      },
    };
  },
  methods: {
    onMapLoaded: async function(map) {
      this.$emit("mapLoaded");
      mapRef = map;
      await this.centerAtUserLocation(map);
    },
    centerAtUserLocation: async function(map) {
      try {
        const pos = await Geolocation.getCurrentPosition();
        mapRef.flyTo({
          center: [pos.coords.longitude, pos.coords.latitude],
          zoom: 13
        });
      } catch (error) {
        console.log(error);
      }
    }
  }
};
</script>

from vue-mapbox-gl.

imfunniee avatar imfunniee commented on May 31, 2024

bro ty @felimartina

from vue-mapbox-gl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.