简体   繁体   English

Google Maps API V3:getBounds()将结果转换为fitBounds()

[英]Google Maps API V3: getBounds() convert result for fitBounds()

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. 我正在使用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. 我知道这是根据文档进行的,因为fitBounds确保在地图上显示给定的边界,以便所有边缘都显示地图内。 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? 之后如何修改getBounds结果以用于fitBounds而没有缩放效果?

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. 用这些术语,告诉我如何计算边距(以及如何为具有getBounds输出的fitBounds计算正确的输入),您的答案将被接受。

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. 因此,问题需要完全按照其要求的方向给出答案。

存储而不是限制地图的缩放和中心,并稍后恢复这些值。

I had the exact same problem and decided to shrink the bounding box resulting from getBounds() with 15%. 我遇到了完全相同的问题,并决定将getBounds()产生的边界框缩小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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM