Comments (5)
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.
Thanks so much! You are a lifesaver.
from vue-mapbox-gl.
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.
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.
bro ty @felimartina
from vue-mapbox-gl.
Related Issues (20)
- Custom Interactive Controls HOT 1
- Mapbox with Quasar; export 'default' (imported as 't') was not found in 'vue'
- cannot get to work with nuxt HOT 1
- v-if not followed while tiles are loading HOT 1
- Trying to use latest 2.0.3 with Mapbox.js error HOT 2
- printing the map issue
- "For larger projects VueMapbox may be a better fit." Don't say that HOT 2
- importing Marker doesn't work HOT 1
- Is there a way to trigger() geolocation programmatically? HOT 1
- Cannot find module 'vue'
- Adding markers stop the map from drawing HOT 4
- Making width 100% so it fills space in responsive screen size HOT 1
- Access to mapbox-gl-draw events
- Safari: [Vue warn]: Error in mounted hook: "ReferenceError: Can't find variable: mapboxgl"
- how to add a geocoder?
- Error: when testing with `jest`
- How to add markers to the map
- mouseenter and mouseleave for fills HOT 1
- error Popup example not fount HOT 1
- How to add custom icons with Markers?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-mapbox-gl.