question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fitBounds to markers

See original GitHub issue

Great Vue plugin!

Apologies if this isn’t a question specific to your plugin, I’m not sure if there’s an easier way to do this. I am struggling figuring out how to retrieve all markers on the map in order to fit the bounds of the map automatically. I’ve looked at this.$ref.map.mapObject._layers, and saw suggestions online to look for layers that have a .feature property, but despite having a marker/markers on the map, I’m not seeing anything that looks like the marker instance. Any suggestions?

I’m also wondering if this will even be possible, if you don’t have a component for FeatureGroup…

<v-map v-if="collection" ref="map" :zoom=-1 :center="[47.413220, -1.219482]">
  <v-tilelayer :url="`https://api.mapbox.com/styles/v1/mapbox/${map_id}/tiles/{z}/{x}/{y}?access_token=${access_token}`" />

  <v-marker-cluster ref="cluster">
    <v-marker
    ref="marker"
    v-for="(model, index) in collection.models"
    v-if="model.acf.location !== null"
    :lat-lng="[model.acf.location.lat, model.acf.location.lng]"
    :key="index">
      <v-popup :content="model.acf.content"></v-popup>
    </v-marker>
  </v-marker-cluster>
</v-map>
this.$refs.map.mapObject.eachLayer(layer => {
  console.log('feature', layer.feature) // undefined
})

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

12reactions
wannymiarellicommented, Jun 27, 2020

@ThomasKientz You are over-complicating it 😃

Look there image https://leafletjs.com/reference-1.6.0.html#latlngbounds

The fitBounds accept an array of [lat,lng] items, so it is as simple as looping over your markers getting lat and long with a simple .map function, as a track

this.$refs.map.mapObject.fitBounds(this.markers.map(m => { return [m.lat, m.lng] }))

0reactions
tripflexcommented, Feb 11, 2021

@wannymiarelli thanks that works, i was doing the same thing … overcomplicating things when not even needed 😛

Read more comments on GitHub >

github_iconTop Results From Across the Web

fitBounds of markers with Leaflet - Stack Overflow
I have a array of markers named markers ...
Read more >
leaflet - How to fit bounds after adding multiple markers
You can instantiate a LatLngBounds object and then extend() it with the coordinates of the companies. After adding the markers you can call...
Read more >
Leaflet.js Tips, Step 3 (markers and fitBounds) - gists · GitHub
Leaflet.js Tips, Step 3 (markers and fitBounds). GitHub Gist: instantly share code, notes, and snippets.
Read more >
Map fit bounds for markers - Google Groups
It seems that the fitBound on a map occurs as a one time event to try and fit all markers on a map,...
Read more >
Help with fitBounds - Maps SDK for Web (JavaScript)
... from goog maps and having trouble understanding how to use fitBounds. ... in case anyone else needs help with fitting markers to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found