4

Right now I am setting the bounds of my react leaflet map by passing a bounds parameter as shown below:

   <div hidden={!this.props.hide && !this.props.toggle} className="map-container">

       <Leaflet.Map ref='leaflet-map' bounds={ this.getBounds()} >

       <Leaflet.TileLayer url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'/>

             { this.geoResults().map(this.renderMarker) }


       </Leaflet.Map>

   </div>

The problem is that sometimes a marker is rendered on the outermost part of the map (in view) so the marker is not even visible unless I drag the map or zoom out one spot. I was trying to fix this with a buffer or trying to plot the bounds then use zoom to zoom out once but nothing seems to work. Do you guys have any ideas?

1

1 Answer 1

12

Solution

You can use boundsOptions attribute of the Map component to pass options to the leaflet's fitBounds() method. In those options you can define padding for example to "pad" the bounds.

From react-leaflet Map documentation:

boundsOptions: object (optional): Options passed to the fitBounds() method.

From leaflet fitBounds options documentation:

padding: Equivalent of setting both top left and bottom right padding to the same value.

Example

So something like this should work (didn't test in action):

<Leaflet.Map
  ref='leaflet-map'
  bounds={this.getBounds()}
  boundsOptions={{padding: [50, 50]}}
/>

Defining your Map element like that should pad the bounds. Tune the padding values to suite your needs.

Another approach

You could add padding to the bounds in your getBounds() function.

2
  • Awesome answer - I have gotten to this part and now I want to add vector polygon on top of the bounds of the map so they can see the exact polygon I am providing in the bounds argument. Can you take a look at my question over here and see if you know the answer to it as well? Thanks! stackoverflow.com/questions/38234377/…
    – maxwell
    Commented Jul 6, 2016 at 22:22
  • 2
    Thanks maxwell. I did make a suggestion for your other problem too. Could you mark my answer as correct and consider upvoting it (that is kinda the idea of gamification of StackOverlow after all). Cheers.
    – Jeewes
    Commented Jul 9, 2016 at 19:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.