-2

I am using map bounds to display markers that fall within the current viewport. Upon zooming or panning the bounds are recalculated and the markers that fall within these bounds are redrawn, effectively hiding any not contained within the viewport. I would like to do it so that the markers draw slightly out of the current viewport, however this would involve extending the bounds equally from all sides rather than using bounds.extend(point). Is this possible?

        //I would like to extend this value in order to draw features that are slightly off the viewport
        var bounds = map.getBounds()

        //This is how I am currently extending the bounds, it works but I am unsure if it is the correct way.

        bounds.b.b = bounds.b.b - 0.5
        bounds.b.f = bounds.b.f + 0.5
        bounds.f.b = bounds.f.b - 0.5
        bounds.f.f = bounds.f.f + 0.5

        //Determining whether the feature lies within the current viewport
        var result = bounds.contains(Featurecenter)

        center = null

        //If the feature lies within the viewport
        if (result) {
            Feature.setMap(map) //Making the feature visible on the map
        }
Maeglin77
  • 173
  • 1
  • 14
  • 1
    What have you tried? The code you posted doesn't show an attempt at extending the bounds in any way. – MrUpsidown Apr 26 '18 at 14:55
  • I haven't found anything to try, in the sense that all examples say to extend the bounds by a certain coordinate which won't work for me. The only idea I have is to manually increase the bounds by changing the coordinates, but it doesn't seem like the best solution. – Maeglin77 Apr 27 '18 at 07:02
  • I added what I tried that works however I am unsure if it is the correct way to carry out this procedure. – Maeglin77 Apr 27 '18 at 07:39

1 Answers1

0
  1. Don't use b, f and such, which are undocumented properties, instead use documented methods such as getNorthEast(), getSouthWest() then lat() and lng() when you need to extract coordinates from a bounds object.

  2. I don't know of any other method to extend a bounds object than to pass new coordinates to it (see below).

  3. The LatLngBounds object has an extend() method that you must use to achieve what you want.

  4. We don't know of "how much" you need to extend the bounds; depending on that, and on the current zoom level when you want to extend your bounds, you will probably need to do some maths.

  5. If you need to extend your bounds by a given (and constant) distance, whatever zoom level is currently set, I suggest you to read some other Q/A that explain how to do that. For example:

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131