简体   繁体   English

Google Maps缩放算法

[英]Google Maps zoom algorithm

In my web application, I have coordinate bounds that I'd like to modify to simulate "zooming in" on google maps. 在我的Web应用程序中,我具有要修改的坐标范围以模拟Google地图上的“放大”。 It seems that zooming back and forth with google maps simply doubles or halves each side of the bounding box. 似乎使用Google地图来回缩放只是将边界框的每一边加倍或减半。 So my function to do this on the server looks like this: 因此,我在服务器上执行此操作的功能如下所示:

private static decimal[] ZoomIn(decimal latMax, decimal lngMin, decimal latMin, decimal lngMax)
{
    decimal lngAdjustment = (lngMax - lngMin) * .25m;
    decimal latAdjustment = (latMax - latMin) * .25m;
    return new[]
    {
        // North
        latMax - latAdjustment,
        // West
        lngMin + lngAdjustment,
        // South
        latMin + latAdjustment,
        // East
        lngMax - lngAdjustment
    };
}

The problem with this approach is the latAdjustment that I'm doing. 这种方法的问题是我正在做的latAdjustment When compared to an actual zoom using the google maps control, this ends up being fairly accurate when zoomed in to city level. 与使用google maps控件进行的实际缩放相比,最终放大到城市级别时相当准确。 However, it is less accurate the further the view is zoomed out. 但是,将视图进一步缩小会降低精度。 I assume this is due to the Mercator projection of the earth that google maps uses. 我认为这是由于谷歌地图使用墨卡托投影的地球。 Is anyone aware of a better formula or method to use to simulate a "zoom"? 有谁知道更好的公式或方法来模拟“缩放”?

Update: My issue has more to do with not knowing the correct formula than google maps. 更新:与Google地图相比,我的问题与不知道正确的公式有关。 Let me illustrate using some sample numbers. 让我用一些样本数字来说明。

Take Chicago, centered at 41.8563226156679,-87.7339862646484 以芝加哥为中心,中心为41.8563226156679,-87.7339862646484

And a bounding box surrounding that point: 并围绕该点的边界框:

  • West: -87.984955197753800 西:-87.984955197753800
  • East: -87.483017331542900 东:-87.483017331542900
  • South: 41.546931599561100 南:41.546931599561100
  • North: 42.164224124684300 北:42.164224124684300

By observing the behavior of google maps, this bounding box zoomed in one level will be: - West: -87.859470731201100 - East: -87.608501798095600 - South: 41.701813184067600 - North: 42.010459667660800 通过观察Google地图的行为,此边界框将放大一级:-西:-87.859470731201100-东:-87.608501798095600-南:41.701813184067600-北:42.010459667660800

(center is kept the same, there is no variance due to mouse movement, etc. I just used the manual zoom button, not the mouse) (中心保持不变,没有由于鼠标移动等引起的变化。我只是使用手动缩放按钮,而不是鼠标)

Using my formula above, longitude will values will come out correct. 使用上面的公式,经度值将正确显示。 Example: 例:

west = west + (west - east) * .25 -87.984955197753800 + ((-87.483017331542900 - -87.984955197753800) * .25) = -87.859470731201075 西=西+(西-东)* .25 -87.984955197753800 +(((-87.483017331542900--87.984955197753800)* .25)= -87.859470731201075

However, the same formula will not work when dealing with latitude. 但是,在处理纬度时,相同的公式将不起作用。 It is near the correct values, but off by just enough that the map shifts noticeably. 它接近正确的值,但相差仅足以使地图明显移动。 This effect is worse with larger bounding boxes and/or when the latitude is further from the equator. 对于较大的边界框和/或当纬度距离赤道较远时,此效果会更糟。 I assume this is due to the mercator projection of the earth. 我认为这是由于地球的墨卡托投影造成的。 Trig class was a long time ago for me, and at this point I'm unable to find a suitable formula for zoom latitude for this situation. Trig课对我来说是很久以前的事情,在这一点上,我无法找到适合这种情况的缩放纬度公式。

Judging by the fact that your testing method uses the mouse, it seems to me that your testing method is the problem. 从您的测试方法使用鼠标这一事实来看,在我看来,您的测试方法就是问题所在。 Since the map zooms in differently based on the centering of the mouse, not the center of the screen, the edge bounds vary, but not the difference between them. 由于地图根据鼠标的中心(而不是屏幕的中心)不同地放大,因此边缘边界会变化,但两者之间的差异不会变化。 This seems to coincide with the details you have given. 这似乎与您提供的详细信息相吻合。

Intuitively, there is nothing wrong with your formula -- just how you tested it. 直观地讲,您的公式没有问题-只是如何测试它。

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

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