-
Notifications
You must be signed in to change notification settings - Fork 0
/
ol-map-polygon-delivery-area.js
79 lines (65 loc) · 2.03 KB
/
ol-map-polygon-delivery-area.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Polygon
*/
var poly = [[20.57, 53.30], [21.24, 53.30], [22.152810, 52.648142], [21.24, 52], [20.50, 52], [19.785212, 52.783446]];
var polygon = new ol.geom.Polygon([poly]);
polygon.transform('EPSG:4326', 'EPSG:3857');
// polygon.applyTransform(ol.proj.getTransform('EPSG:4326', 'EPSG:3857'))
var feature = new ol.Feature(polygon);
// Create vector source and the feature to it.
var PolygonSource = new ol.source.Vector();
PolygonSource.addFeature(feature);
// Create vector layer attached to the vector source.
var PolygonLayer = new ol.layer.Vector({
source: PolygonSource
});
// Polygon color
var style = new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(71, 166, 255, 0.1)',
weight: 1
}),
stroke: new ol.style.Stroke({
color: 'rgba(71, 166, 255, 1)',
width: 2,
lineDash: [3,6,9]
})
});
PolygonLayer.setStyle(style);
// Layer z-index
PolygonLayer.setZIndex(1);
// Add the vector layer to the map.
map.addLayer(PolygonLayer);
/*
Test js point in poligon
*/
function TestDeliveryArea(point, vs)
{
var x = point[0], y = point[1];
var inside = false;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
var xi = vs[i][0], yi = vs[i][1];
var xj = vs[j][0], yj = vs[j][1];
var intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
};
if(TestDeliveryArea([21.002902, 52.228850], poly))
{
console.log("Marker in delivery area (polygon) !!!");
}
/*
Is coordinate in polygon
var coordinate = [1,2]
var polygon_points_array = [[20.57, 53.30], [21.24, 53.30], [22.152810, 52.648142], [21.24, 52], [20.50, 52], [19.785212, 52.783446]];
*/
function PointInPolygon(polygon_points_array, coordinate)
{
var polygon = new ol.geom.Polygon([polygon_points_array]);
var out = polygon.intersectsCoordinate(coordinate);
console.log("Polygon in area: ", out);
return out;
}
PointInPolygon(poly, [21.002902, 52.22885]);