1

NO, THE ABOVE ANSWERS DON'T ANSWER MY QUESTION. PLEASE READ MY UPDATE BELOW TO SEE THE CLARIFICATION WHY THIS IS A DIFFERENT CASE!!!

I'm using Google maps API V3. When I write this code:

map.fitBounds(map.getBounds());

the map zooms out!

I understand that it's according to the documentation since fitBounds ensures to show the given bounds on the map so that all the edges are shown inside the map. Therefore, the answer I'm looking for lies into the following:

How to modify the result of getBounds to be used for fitBounds without zoom effect afterwards?

Basically, I'm quite sure this can be calculated because that's what the map does, but in the opposite direction, when adding margins, to show the given bounds completely. In these terms, tell me how to calculate the margins (and therefore, how to calculate the correct input for fitBounds having output from getBounds) and your answer will be accepted.

Thanks in advance!

Update:

Zoom and Center retrieval and setting does not work for me! here's the reason why:

I am going to hold the map's viewport information in the database. Later on, I want to re-create the map and show the exact same location again. However, the mapping platform can differ from user to user. Therefore, zoom and center is not standard between different mapping platforms/frameworks and cannot be used in all them with the same results. Therefore, the only standard way is to hold the bounds in the database. So the question needs an answer exactly in the direction it is asking for.

Tengiz
  • 8,011
  • 30
  • 39
  • For which purposes? When you don't want to change the viewport, do simply nothing. Or did you store the bounds and use them later to restore a previous viewport? – Dr.Molle Oct 26 '13 at 20:41
  • This was to simpler explain the calculation need. In reality, I need to store the current viewport in the database, and then show it back once the page is accessed again. – Tengiz Oct 26 '13 at 20:42
  • possible duplicate of [Google Maps V3 - How to calculate the zoom level for a given bounds](http://stackoverflow.com/questions/6048975/google-maps-v3-how-to-calculate-the-zoom-level-for-a-given-bounds) – geocodezip Oct 26 '13 at 20:59
  • I don't understand what's wrong with downvoters. I hope SO still has people who are capable of reading the question and just answering it. I think the question is clear enough to understand what I need as an answer. – Tengiz Oct 26 '13 at 21:34
  • @geocodezip no, unfortunately this is not a duplicate. I cannot go along with zoom level. I need to show the EXACT bounds that are shown on the map. Zoom levels are integer and have gaps between them. Also, please read my update that clarifies more. – Tengiz Oct 26 '13 at 21:38
  • possible duplicate of [Google Map API v3 — set bounds and center](http://stackoverflow.com/questions/1556921/google-map-api-v3-set-bounds-and-center) – david strachan Nov 08 '13 at 20:27

2 Answers2

1

Store instead of the bounds the zoom and center of the map and restore these values later.

Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
  • That does not work, because in that case I'm bound to the Google maps platform. There's a need to store the data in a standard format that can be shown in any kind of map. Google's zooming system is not a widely accepted standard. – Tengiz Oct 26 '13 at 20:45
  • Then store bounds,zoom and center and restore the values depending on the used platform. – Dr.Molle Oct 26 '13 at 20:47
  • Exactly. Tell me how to restore it for Google platform. That's the question. Storing a zoom is not an option. sorry. – Tengiz Oct 26 '13 at 20:48
  • To explain in more details why this solution does not work (because it may seem I'm just stubborn and don't want to accept): imagine the location was saved from another mapping platform. In such case, even if we have zoom level and bounds in the DB, only the bounds are holding valuable information. Zoom levels are specific to the concrete mapping platforms. – Tengiz Oct 26 '13 at 21:11
  • bounds are more specific...to the size of the map. Assuming that all maps on all platforms have the same size, it would be sufficient when you increase the zoom on a google-maps-implementation after using fitBounds() – Dr.Molle Oct 26 '13 at 21:25
  • Let me clarify: let's say in the DB I have zoom:3. Let's say that comes from a different mapping platform. And my requirement is to show exactly the same bounds on the page with the Google map on it. There's no guarantee that one of the integer zoom levels that Google map has, will exactly match with the zoom level that came from the DB. And yes, the sizes of maps are exactly the same. – Tengiz Oct 26 '13 at 21:30
0

I had the exact same problem and decided to shrink the bounding box resulting from getBounds() with 15%. That does the job nicely for me:

var neLat = map.getBounds().getNorthEast().k;
var neLong = map.getBounds().getNorthEast().B;
var swLat = map.getBounds().getSouthWest().k;
var swLong = map.getBounds().getSouthWest().B;

var boundingBoxPerc = 0.15;
var mapWidth = neLong - swLong;
var mapHeight = neLat - swLat;

var ne = new google.maps.LatLng(neLat - mapHeight * boundingBoxPerc, neLong - mapWidth * boundingBoxPerc);
var sw = new google.maps.LatLng(swLat + mapHeight * boundingBoxPerc, swLong + mapWidth * boundingBoxPerc);
map.fitBounds(new google.maps.LatLngBounds(sw, ne));
Pascal Lindelauf
  • 4,782
  • 4
  • 40
  • 55