查找坐标是否在多边形内

问题描述 投票:0回答:1

我想知道给定的坐标(纬度和经度)是否在给定的4个坐标内。

enter image description here

即,如果我有A,B,C,D和E的经度,我想检查E是否在A,B,C和E内。

我该怎么做?

我试图从Google Map Coordinates LatLngBound class - contains看这个例子。

我可以看到的示例就像

var bounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(54.69726685890506,-2.7379201682812226),
    new google.maps.LatLng(55.38942944437183, -1.2456105979687226)
);
var center = bounds.getCenter();  // still returns (55.04334815163844, -1.9917653831249726)
var x = bounds.contains(center);  // now returns true

它只有2个坐标,例如A&B。没有C&D的坐标怎么可能?

javascript google-maps location latitude-longitude google-location-services
1个回答
0
投票
因为这是使用LatLngBounds来创建矩形,而不是多边形。如果一个框有两个角,则可以假定其他两个角。您只需要指定相反的角即可。在您的示例中,您只需要成对发送A和D或B和C,其他两个可以忽略。如果需要多边形,则要创建a polygon,然后确定它是否包含带有google.maps.geometry.poly.containsLocation(point, polygon)的点。

Documentation here

Example here.

© www.soinside.com 2019 - 2024. All rights reserved.