I need to show a marker for each user that falls within the visible portion of google map. To do so, I get the bounding box from google map, and send it down to the database and I receive a list of users that fall within the box. Then loop through the list of users and place a marker on the map for each user.

All works well, however, if the lat/lng falls just bellow the top border, then only the bottom of the marker is visible. If the lat/lng falls next to the side borders, then only 1/2 of the marker is visible.

One way to tackle this problem is to send a reduced version of the bounding box to the database. This way, you'd have a bounding box with margins, but you don't show anything within the margins.

Now, what is the best way of shrinking down the bounding box before sending it down to the database?

Is there any better way of doing this?

Check the image, 1/2 of the marker is hidden. (top) enter image description here

  • I don't understand your question or its context, can you elaborate - perhaps with a diagram of what you're attempting to achieve?
    – nagytech
    Mar 3, 2013 at 6:16
  • Take 2. Hope this better explains the problem I am trying to solve. All I need is to calculate a new bounding box that is smaller than what I get from google and fits within it. Mar 4, 2013 at 15:02
  • Found one may related with this question:stackoverflow.com/questions/34894732/…
    – bf39L
    Jul 25, 2016 at 23:34

4 Answers 4


First I suggest you to take a look at the docs of the geometry library

Then here are the steps that are recommended:

  1. Measure the horizontal and vertical dimensions of the bounds.
  2. Multiply both of them by 0.1 thus you will get the 10% value of each.
  3. Calculate the distance you will use to offset the new bounds' corner coordinates by calculating the hypotenuse of the triangle where the two legs are the two 10% values. So it will look like leg1 to the pwr of 2 + leg2 to the pwr of 2 = hypotenuse to the pw of 2 Simple pithagoras theorem.
  4. Now that you have the hypotenuse value, you can use that as an offset distance from the two corners of the bounds: the southwestern and northeastern. Use > computeOffset(from:LatLng, distance:number, heading:number, radius?:number) from the google library i referenced above. When calculating the offset by the southwestern corner, use a heading value of 45° and by the northeastern, use 225°
  5. The above steps will result in having two new latlngs that you can use to form your new, reduced bounds object. You can test the functionality by drawing rectangle objects, using these bounds.

I think it will be exactly what you need.

  • This isn't very efficient, there's simply no need to bring Math.pow into the equation.
    – nagytech
    Mar 11, 2013 at 10:28
  • 1
    Personally, I don't think that two dead simple pithagoras theorem could seriously make this approach inefficient. Do you have any other approach to share? Mar 11, 2013 at 11:24
  • It's not efficient because there's no need to go on a tangent, literally. It may not be relevant to this exact problem, but if you follow the path of least resistance in computing science and you won't go very far - demand in applications can scale quite quickly without warning. I already have provided my solution. Feel free to down vote it for arbitrary reasons of your own. Also, google.com.au/search?q=pithagoras
    – nagytech
    Mar 11, 2013 at 22:59
  • @nagytech how about providing a more efficient answer then? Sep 23, 2021 at 13:07
  • @hugoderhungrige I did provide a solution... in 2013. It was ignored, go figure.
    – nagytech
    Mar 18, 2023 at 4:02

I would try the fitBounds method in the API first: https://developers.google.com/maps/documentation/javascript/reference#Map

The way you suggest would work, but the math to do it is not going to be that easy.


If the root of this question is to avoid the margin used by Google Map API's fitBounds() function then I suggest to look at Google Maps V3 - How to calculate the zoom level for a given bounds question on StackOverflow. It gives you a function to directly calculate the zoom level you need to use to fit the given bounds into the visible area of the map. This is a better trick, than reducing the bounding box to counteract the effect of the mandatory margin used fitBounds().


I have found an answer at Stack Overflow that may related to this question and I think the method it is using is same as @Tamas said.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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