简体   繁体   English

获取多边形的边界框

[英]Get bounding box of polygon

I have many complicated polygons some with 750+ points. 我有许多复杂的多边形,有些有750+点。 Is there an fast and efficient way to get bounding box? 是否有快速有效的方法来获得边界框? I would hate to loop throught every point and expand the bounding box.. 我不想在每个点上循环并扩展边界框。

Solution should be in or maybe there's a Google Maps API v3 function that I've missed. 解决方案应该是或者可能是我错过的Google Maps API v3功能。


Or should I hardcode the coordinates of bounding box and use these to decrease the load on client? 或者我应该硬编码边界框的坐标并使用这些来减少客户端的负载?

在此输入图像描述

How polygons are made: 如何制作多边形:

//Coordinates
var coordinates = [ 
    new google.maps.LatLng(11,22),
    new google.maps.LatLng(11,22),
    new google.maps.LatLng(11,22),

    //etc up to 200, 500 or even 800 points
]

//Options
var options = {
    path: coordinates,
    strokeColor: "#222",
    strokeOpacity: 1,
    strokeWeight: 2,
    fillColor: "#000",
    fillOpacity: 0,
    zIndex: 0
}

//Create polygon
var polygon = new google.maps.Polygon( options );

//Show it on map
polygon.setMap( map );

I need to do my homework because live calculations are excluded for sure. 我需要做我的作业,因为实时计算被排除在外。 I will probably need to do it the hard way but maybe some of you know some handy online tool which calculates bordering box based on inserted coords? 我可能需要以艰难的方式去做,但也许你们中的一些人知道一些方便的在线工具,根据插入的坐标计算边界框?

I need as simple as possible shape because I need to check if my polygon is in viewport and it would probably be a nightmare with 800 points because I don't know any other way besides looping through all the points. 我需要尽可能简单的形状,因为我需要检查我的多边形是否在视口中,它可能是一个800点的噩梦,因为除了循环所有点之外我不知道任何其他方式。

Polygon has not a method getBounds on Google Maps API v3. Polygon在Google Maps API v3上没有方法getBounds You can implement it manually. 您可以手动实现它。 But it contains fors. 但它包含了fors。 By the way. 顺便说说。 I have implemented getBounds method. 我已经实现了getBounds方法。 It is a hard coded version. 这是一个硬编码版本。 Link for the demo . 演示链接。

UPDATE UPDATE

To get single border box for several polygons use union method of getBounds method. 要获得多个多边形的单边框,请使用getBounds方法的union方法。

var coordinates = [ 
   new google.maps.LatLng(10,15),
   new google.maps.LatLng(12,16),
   new google.maps.LatLng(11,18),
   new google.maps.LatLng(11,19),
   new google.maps.LatLng(13,21),
   new google.maps.LatLng(12,22),
   new google.maps.LatLng(13,24),
   new google.maps.LatLng(11,25),
   new google.maps.LatLng(8,23),
   new google.maps.LatLng(7,23),
   new google.maps.LatLng(8,21),
   new google.maps.LatLng(6,17),
   new google.maps.LatLng(9,16)
]

var coordinates_1 = [ 
   new google.maps.LatLng(15,28),
   new google.maps.LatLng(16,30),
   new google.maps.LatLng(17,30),
   new google.maps.LatLng(16,31),
   new google.maps.LatLng(16,32),
   new google.maps.LatLng(14,29),
]

var options = {
   path: coordinates,
   strokeColor: "#222",
   strokeOpacity: 1,
   strokeWeight: 2,
   fillColor: "#000",
   fillOpacity: 0,
   zIndex: 0,
   map: map
}

var options_1 = {
   path: coordinates_1,
   strokeColor: "#222",
   strokeOpacity: 1,
   strokeWeight: 2,
   fillColor: "#000",
   fillOpacity: 0,
   zIndex: 0
}

var polygon = new google.maps.Polygon(options);
var polygon_1 = new google.maps.Polygon(options_1);

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

var rectangle = new google.maps.Rectangle({
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    map: map,
    bounds: polygon.getBounds()
  });

var rectangle_1 = new google.maps.Rectangle({
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    map: map,
    bounds: polygon_1.getBounds()
  });

var rectangle_single = new google.maps.Rectangle({
    strokeColor: '#FFC000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FFF000',
    fillOpacity: 0.35,
    map: map,
    bounds: polygon.getBounds().union(polygon_1.getBounds())
  });

A simpler and more compact version of the code is: 一个更简单,更紧凑的代码版本是:

    google.maps.Polygon.prototype.getBounds = function() {
        var bounds = new google.maps.LatLngBounds();
        this.getPath().forEach(function(element,index){ bounds.extend(element); });
        return bounds;
    }

X and Y coordinates in an array then x and y array min and max. 数组中的X和Y坐标,然后是x和y数组min和max。 Though we need more then a picture to know what you exactly want and how. 虽然我们需要更多的图片才能知道你究竟想要什么以及如何。

If the points are given we no a priori structure, there is no more efficient way than iterating over the points and expanding the bounding box. 如果给出的点我们没有先验结构,那么没有比迭代点和扩展边界框更有效的方法 Since you do not know anything in advance, any point can be a corner point (or edge point ) and therefore you have to take it into consideration. 由于您事先不知道任何事情,因此任何点都可以是角点 (或边缘点 ),因此您必须将其考虑在内。 No algorithm can do it more efficiently. 没有算法可以更有效地做到这一点。

If however you have control about the server that provides the coordinates, you can store the points in advance in a datastructure like a kd tree . 但是,如果您可以控制提供坐标的服务器,则可以将这些点预先存储在像kd树这样的数据结构中 Say for instance all the points are static, you can store them in such kd tree, and then you can defintely speed up things (although, as far as I know the problem is still O(n) , but you do not have to take all points into account). 比如说所有的点都是静态的,你可以将它们存储在这样的kd树中,然后你就可以快速加速了事情(虽然据我所知,问题仍然是O(n) ,但你不必采取所有要点都考虑在内)。

The problem is however that you do not provide any information about the server side. 但问题是您没有提供有关服务器端的任何信息。

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

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