【问题标题】:Centering Google Maps depending on pins根据图钉居中谷歌地图
【发布时间】:2011-12-30 14:41:08
【问题描述】:

我有一个包含谷歌地图的 ASP.NET 网站。我正在使用由一些聪明而乐于助人的人制作的 GoogleMapForASPNet 框架。

任何人,我在插入两个图钉后无法居中地图。

基本上,我正在计算图钉的中点并将其设置为地图的中心点。

下面是我的代码:

GooglePoint newPoint = new GooglePoint();
        double newLat = 0;
        double newLong = 0;

        if (googlePointA.Latitude > googlePointB.Latitude)
        {
            newLat = googlePointA.Latitude - googlePointB.Latitude;
            newPoint.Latitude = googlePointA.Latitude - newLat;
        }
        else
        {
            newLat = googlePointB.Latitude - googlePointA.Latitude;
            newPoint.Latitude = googlePointB.Latitude + newLat;
        }
        if (googlePointA.Longitude > googlePointB.Longitude)
        {
            newLong = googlePointA.Longitude - googlePointB.Longitude;
            newPoint.Longitude = googlePointA.Longitude - newLong;
        }
        else
        {
            newLong = googlePointB.Longitude - googlePointA.Longitude;
            newPoint.Longitude = googlePointB.Longitude + newLong;
        }

        GoogleMapForASPNet1.GoogleMapObject.CenterPoint = newPoint;
        GoogleMapForASPNet1.GoogleMapObject.ZoomLevel = 8;

它工作一半一半,但不正确。比如,当我输入不同的 Pins 时,它并没有真正使地图居中,但足够接近。或者有时,另一个大头针会离开地图,但只有一英寸,这意味着地图根本没有居中。

缩放是静态的,因为大头针总是在附近,所以我不需要让它变成动态的。

非常非常非常感谢任何帮助。

谢谢。

【问题讨论】:

    标签: google-maps


    【解决方案1】:

    Google Maps API 有一个对象可以做到这一点:google.maps.LatLngBounds

    只需创建一个LatLngBounds 对象,使用extend() 方法将您的引脚坐标添加到其中,然后使用地图panToBounds(yourBounds) 方法就可以了!

    【讨论】:

    • 呃。我正在使用一个名为 GoogleMapForASPNet 的预构建框架。我在这里下载了它:shabdar.org/asp-net/… 所以我必须使用它的内置功能。显然,它没有 LatLngBounds。那么......我该怎么做呢?有了这个框架。
    • alcfeoh。我 100% 确定我没有得到正确结果的唯一原因是因为我的计算。不是因为没有使用谷歌的其他API什么的……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多