谷歌地图Javascript和跨越+/- 180子午线的多边形一起使用 [英] Google Maps Javascript and working with Polygons crossing +/-180 meridian

查看:167
本文介绍了谷歌地图Javascript和跨越+/- 180子午线的多边形一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google地图上的多边形时,可能会出现多边形将跨越地图重叠的情况。例如,当前往地图的右侧边缘,并放置点以创建一个方框时,地图右侧的左侧点和地图重复左侧的右侧点(如地图可以无限平移)。

也就是说,我会看到接近+180和-180极值的点,例如:

  [50,178]左上角
[50,-178]右上角
[-50,-178]右下角
[-50,178]左下角

这是从循环多边形.getPath()分。

  google.maps.event.addListener(drawingManager,'polygoncomplete',function(polygon){
polySelected = true;
coords = new Array();
polygon.getPath()。forEach(function(l,index){
coords [index] =( l.lat()+,+ l.lng()+)
});
}

有了这些数据,就不可能判断这些点是否实际上穿过了180个子午线n行。如果我拿出'左上','右上',你将无法辨别用户的第二点是在右边(穿过180)还是左边(制作一个非常大的盒子)。



有没有办法解决这个问题?比如没有像这样的'包裹'坐标[50, 182 ]?什么是处理穿越边界和获得数据的标准方式?

谢谢!

编辑 - 这不是关闭多边形并使坐标捕捉到很大区域的情况。这种情况适用于正常的多边形手动闭合,创建一个小的方形多边形,但是横穿地图水平重复并且具有合成坐标的一个区域与穿过重复180子午线的多边形或者非常宽的多边形不可区分没有不同的垂直)并覆盖相反的区域。我希望我可以演示图片来帮助解释,但我还没有这个功能。



回答
我没有看到一种回答我自己的问题的方式,但是我已经知道并希望分享。我意识到你不能绘制纵向延伸超过180度的多边形的边缘。知道这一点,如果你有两个不同的点数大于180,它会穿过+/- 180子午线。

感谢所有提供支持的人! / b>

解决方案

您可以这样做:

  var longitude = l.lng(); 
(经度> 180)
经度 - = 360; //例如182和542变成-178,这是等效经度
,而(经度<-180)
经度+ = 360;

coords [index] =(+ l.lat()+,+ longitude +);

只要您的纬度在90到-90之间。如果不是,你也需要解决这个问题......让我知道你是否需要帮助。


When working with polygons on google maps, there can be cases where a polygon will cross the map overlap. For example, when going to the right edge of the map, and placing points to create a box, with the left points on the right edge of the map, and the right points on the left edge of the repetition of the map (as the map can pan infinitely horizontally).

That said, I would see points close to the +180 and -180 extremes such as:

[50, 178] top left
[50, -178] top right
[-50, -178] bottom right
[-50, 178] bottom left

This is the data provided when pulled out from looping the polygon.getPath() points.

google.maps.event.addListener(drawingManager, 'polygoncomplete', function(polygon) {
polySelected = true;
coords = new Array();
polygon.getPath().forEach(function (l, index) {
coords[index]="("+l.lat()+","+l.lng()+")"
});
}

With this data, it is impossible to tell if the points actually cross the 180 meridian line. If I took out the 'upper left', 'upper right', you would have no way of discerning if the users second point was to the right (crossing 180) or left (making a very large box).

Is there any way around this? Such as not having 'wrapped' coordinates [50, 182] like that? What is the standard way of dealing with crossing the boundaries and having data representing that?

Thanks!

Edit - This is not a case of closing the polygon and having coordinates snap to a very large area. This case is for a normal polygon manual closure, creating a small square polygon, but one that crosses the map horizontal repeat and having the resultant coordinates be indistinguishable from a polygon that crosses the repeat 180 meridian line, or one that is very wide (but no different vertically) and covering the opposite area. I wish I could demonstrate with pictures to help explain but I do not have this functionality yet.

Answer I do not see a way to answer my own question, however I have figured it out and want to share. I realized that you cannot draw an edge of a polygon that extends more than 180 degrees longitudinally. Knowing that, if you have two points that have a different of greater than 180, it crosses the +/-180 meridian.

Thanks to all who were offering support!

解决方案

You can do this:

var longitude = l.lng();
while (longitude > 180)
    longitude -= 360; // e.g. 182 and 542 become -178, which is the equivalent longitude
while (longitude < -180)
    longitude += 360;

coords[index]="("+l.lat()+","+longitude+")";

As long as your latitude is between 90 and -90. If it isn't you need to fix that too... let me know if you need help with that.

这篇关于谷歌地图Javascript和跨越+/- 180子午线的多边形一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆