如何让 Google 地图显示整个多边形?

发布于 2024-08-20 13:06:57 字数 297 浏览 10 评论 0 原文

我有一组 GPolygon 对象,它们附加到我的域模型中的对象,并添加到我的页面上的地图中。域对象显示在同一页面上的列表中,当用户单击其中一个时,我想显示关联的多边形。

我希望显示整个多边形,理想情况下我希望地图以多边形中心为中心。

我希望能有一个地图 API 调用,

myMap.makeSureYouCanSeeAllThisPolygon(aPolygon);

但我一直没能找到。

如果我必须手动执行此操作,那么我显然可以轻松地确定居中,但是如何确定缩放呢?

I have a set of GPolygon objects which are attached to objects in my domain model and are added to a map on my page. The domain objects are being shown in a list on the same page and when the user clicks on one of them I want to show the associated polygon.

I want the whole polygon to be showing and ideally I want the map to be centred on the polygon centre.

I was hoping that there would be a maps API call along the lines of...

myMap.makeSureYouCanSeeAllThisPolygon(aPolygon);

but I haven't been able to find one.

If I have to do this manually, then I can obviously figure out the centering easily, but how do I figure out the zoom?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

两人的回忆 2024-08-27 13:06:57

Google Maps v3 API 本身不支持 google.maps.Polygon 类的 getBounds() 方法。这看起来很奇怪,因为 google.maps.Map 类有一个 fitBounds() 方法,这正是您正在寻找的。如果您将 Google Maps API 与任何类型一起使用那么这应该在你的技巧包中::

google.maps.Polygon 类的 getBounds() 方法

google.maps.Polygon.prototype.getBounds = function() {
    var bounds = new google.maps.LatLngBounds();
    var paths = this.getPaths();
    var path;        
    for (var i = 0; i < paths.getLength(); i++) {
        path = paths.getAt(i);
        for (var ii = 0; ii < path.getLength(); ii++) {
            bounds.extend(path.getAt(ii));
        }
    }
    return bounds;
}

有了这个方法,就可以非常简单地在地图上居中并拟合多边形。

注意:如果您不熟悉 getAt()getLength() 也没关系,它们是 google.maps.MVCArray 类特有的方法。当您调用多边形的 getPaths() 方法时,它会将您的 LatLng 数组作为 LatLng 的可变 MVCArray 返回。您可以在此处准备有关 MVCArrays 的信息 - Google 地图 v3 API MVCArray 类

继续前进。这是框架,您必须在下一段代码之前的某个地方实现上面的原型方法。

var map = new google.maps.Map(container, opts); // I'll spare the details on this

var coords = [
    new google.maps.LatLng(25.774252, -80.190262)
    ,new google.maps.LatLng(18.466465, -66.118292)
    ,new google.maps.LatLng(32.321384, -64.75737)
    ,new google.maps.LatLng(25.774252, -80.190262)
];

var myPolygon = new google.maps.Polygon({
    paths: coords
    ,strokeColor: "#A80000"
    ,strokeOpacity: 0.8
    ,strokeWeight: 1
    ,fillColor: "#0b2a32"
    ,fillOpacity: 0.12
});

舞台设置完毕后,您只需将此(或任何)多边形居中并缩放即可::

map.fitBounds(myPolygon.getBounds());

Google Maps v3 API doesn't natively support a getBounds() method for the google.maps.Polygon class. This seems strange given the fact that the google.maps.Map class has a fitBounds() method, which is exactly what you're looking for.. If you use the Google Maps API with any kind of frequency then this should be in your bag of tricks ::

getBounds() method for the google.maps.Polygon class

google.maps.Polygon.prototype.getBounds = function() {
    var bounds = new google.maps.LatLngBounds();
    var paths = this.getPaths();
    var path;        
    for (var i = 0; i < paths.getLength(); i++) {
        path = paths.getAt(i);
        for (var ii = 0; ii < path.getLength(); ii++) {
            bounds.extend(path.getAt(ii));
        }
    }
    return bounds;
}

Having this method on hand makes it very simple to center and fit a polygon on the map.

Note : If you're not familiar with getAt() and getLength() that's fine, they're methods unique to the google.maps.MVCArray class. When you call the getPaths() method of a polygon it returns your array of LatLng's as a mutable MVCArray of LatLng's.. you can ready about MVCArrays here - Google Maps v3 API MVCArray class.

Moving on. Here's the framework, you'll have to implement the prototyped method above somewhere before this next bit of code.

var map = new google.maps.Map(container, opts); // I'll spare the details on this

var coords = [
    new google.maps.LatLng(25.774252, -80.190262)
    ,new google.maps.LatLng(18.466465, -66.118292)
    ,new google.maps.LatLng(32.321384, -64.75737)
    ,new google.maps.LatLng(25.774252, -80.190262)
];

var myPolygon = new google.maps.Polygon({
    paths: coords
    ,strokeColor: "#A80000"
    ,strokeOpacity: 0.8
    ,strokeWeight: 1
    ,fillColor: "#0b2a32"
    ,fillOpacity: 0.12
});

With the stage set, all you'd have to do to center and zoom on this (or any) polygon is ::

map.fitBounds(myPolygon.getBounds());
不知所踪 2024-08-27 13:06:57

我编写了一个函数来添加到 Kevin James 解决方案中。它需要一个多边形数组并获取地图上可能拥有的所有多边形的边界。你只需要把它们推到一个数组上,然后你就可以调用我的函数来自动缩放地图。

function getArrayBounds(polyArray){
    var bounds = new google.maps.LatLngBounds();
    var path, paths; 
    for(var polys = 0; polys < polyArray.length; polys++) {
        paths = polyArray[polys].getPaths();       
        for (var i = 0; i < paths.getLength(); i++) {
            path = paths.getAt(i);
            for (var ii = 0; ii < path.getLength(); ii++) {
                bounds.extend(path.getAt(ii));
            }
        }
    }
    return bounds;
}

I wrote a function to add to Kevin James solution. it takes a polygon array and gets the bounds for all the polygons you may have on your map. you just need to push them onto an array and then you can call my function to auto zoom the map.

function getArrayBounds(polyArray){
    var bounds = new google.maps.LatLngBounds();
    var path, paths; 
    for(var polys = 0; polys < polyArray.length; polys++) {
        paths = polyArray[polys].getPaths();       
        for (var i = 0; i < paths.getLength(); i++) {
            path = paths.getAt(i);
            for (var ii = 0; ii < path.getLength(); ii++) {
                bounds.extend(path.getAt(ii));
            }
        }
    }
    return bounds;
}
花开柳相依 2024-08-27 13:06:57

您可以使用 aPolygon.getBounds().getCenter(); 获取多边形的中心点,然后使用 myMap.setCenter( 返回的 GLatLng 方法。

要获取缩放级别,您可以使用 myMap.getBoundsZoomLevel(aPolygon.getBounds()); 然后将其与 myMap.setZoom() 方法一起使用。

You can get the centre point of a polygon by using aPolygon.getBounds().getCenter(); and then using the GLatLng returned with the myMap.setCenter() method.

To get the zoom level, you can use myMap.getBoundsZoomLevel(aPolygon.getBounds()); then use this with myMap.setZoom() method.

焚却相思 2024-08-27 13:06:57

认为他说:

map.fitBounds(myPolygon.my_getBounds());

工作是一种享受。

think he ment:

map.fitBounds(myPolygon.my_getBounds());

works a treat.

最偏执的依靠 2024-08-27 13:06:57

该解决方案适用于我已成功添加到地图中的多边形。为 google.maps.Polygon 类添加 getBounds() 方法,然后使用 map.fitBounds(myPolygon.getBounds());

This solution worked for the polygons I already successfully added to the map. Adding the getBounds() method for the google.maps.Polygon class, and then using map.fitBounds(myPolygon.getBounds());

美胚控场 2024-08-27 13:06:57

用 ES6 编写的 getBounds 函数的较短版本

google.maps.Polygon.prototype.getBounds = function () {
    let bounds = new google.maps.LatLngBounds();
    this.getPaths().forEach(p => {
        p.forEach(element => bounds.extend(element));
    });
    return bounds;
}

使地图居中

map.fitBounds(poly.getBounds());

A shorter version from the getBounds function written in ES6

google.maps.Polygon.prototype.getBounds = function () {
    let bounds = new google.maps.LatLngBounds();
    this.getPaths().forEach(p => {
        p.forEach(element => bounds.extend(element));
    });
    return bounds;
}

To center the map

map.fitBounds(poly.getBounds());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文