-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenlayers-demo2.html
146 lines (130 loc) · 5.16 KB
/
openlayers-demo2.html
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>OpenLayers Tutorials: Query a feature layer (spatial)</title>
<style>
html,
body,
#map {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #323232;
}
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.15.1/css/ol.css" type="text/css" />
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.15.1/build/ol.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/olms.js" type="text/javascript"></script>
<script src="https://unpkg.com/@esri/[email protected]/dist/bundled/request.umd.js"></script>
<script src="https://unpkg.com/@esri/[email protected]/dist/bundled/feature-service.umd.js"></script>
<script src="https://unpkg.com/[email protected]"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/src/ol-popup.css" />
</head>
<body>
<div id="map"></div>
<script>
const apiKey = "YOUR_API_KEY";
const map = new ol.Map({
target: "map"
});
map.setView(
new ol.View({
center: ol.proj.fromLonLat([-77.0369, 38.8872]),
zoom: 12.5
})
);
const basemapId = "ArcGIS:StreetsNight";
const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey;
const schoolLayerStyle = (feature) => {
return new ol.style.Style({
image: new ol.style.Icon({
src: "https://creazilla-store.fra1.digitaloceanspaces.com/cliparts/1410399/school-house-clipart-xl.png",
scale: 0.017
})
})
}
const polygonLayerStyle = (feature) => {
return new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgba(255, 182, 18)',
width: 3,
}),
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.3)',
})
})
}
olms(map, basemapURL).then(function (map) {
// Two empty vector tile layers
const pointLayer = new ol.layer.Vector({
source: new ol.source.Vector(),
style: schoolLayerStyle
})
const interactionLayer = new ol.layer.Vector({
source: new ol.source.Vector(),
style: polygonLayerStyle
});
map.addLayer(pointLayer);
map.addLayer(interactionLayer);
//Draw Interaction, connected to your interactionLayer. By setting its type to Polygon, it only allows polygon features to be drawn.
const drawInteraction = new ol.interaction.Draw({
source: interactionLayer.getSource(),
type: "Polygon",
stopClick: true // don't fire "click" events (needed later)
});
map.addInteraction(drawInteraction);
drawInteraction.on("drawend", (e) => {
const feature = new ol.format.EsriJSON().writeFeatureObject(e.feature, {
featureProjection: map.getView().getProjection()
});
executeQuery(feature.geometry);
drawInteraction.setActive(false);
});
//Specify JSON as the return type and specify returnGeometry. All of the features within the geometry will be returned.
function executeQuery(geometry) {
arcgisRest
.queryFeatures({
url: "https://services7.arcgis.com/D3ldBDYkJEQ9Y4At/arcgis/rest/services/public_school_characteristics_202021/FeatureServer/0",
geometry: geometry,
geometryType: "esriGeometryPolygon",
spatialRel: "esriSpatialRelContains",
f: "json",
inSR: 4326,
returnGeometry: true
})
//Use Esri JSON feature format to set returned points as pointLayer's source
.then((response) => {
const features = new ol.format.EsriJSON().readFeatures(response);
pointLayer.getSource().addFeatures(features);
});
}
const popup = new Popup();
map.addOverlay(popup);
map.on("click", (event) => {
let school = map.getFeaturesAtPixel(event.pixel, {
layerFilter: (l) => l === pointLayer
})[0];
if (school) {
// user clicked on a school to see more information about it
const message =
`<b>School Name: </b>${school.get("SCH_NAME")}` +
`<br><b>Num of Students: </b>${school.get("TOTAL")} <br>`
popup.show(event.coordinate, message);
drawInteraction.abortDrawing();
} else {
// user clicked elsewhere on the map to reset
popup.hide();
pointLayer.getSource().clear();
interactionLayer.getSource().clear();
drawInteraction.setActive(true);
}
});
});
</script>
</body>
</html>