Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dispatch events for area and geojson changes beyond drawMode #69

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,27 @@
map.addEventListener("ready", (event) => {
console.log("map ready");
});

// applicable when drawMode is enabled
map.addEventListener("areaChange", ({ detail: area }) => {
console.debug({ area });
});
map.addEventListener("geojsonChange", ({ detail: geojson }) => {
console.debug({ geojson });
});

// applicable when showFeaturesAtPoint is enabled
map.addEventListener("featuresAreaChange", ({ detail: featuresArea }) => {
console.debug({ featuresArea });
});
map.addEventListener("featuresGeojsonChange", ({ detail: featuresGeojson }) => {
console.debug({ featuresGeojson });
});

// applicable when geojsonData is provided
map.addEventListener("geojsonDataArea", ({ detail: geojsonDataArea }) => {
console.debug({ geojsonDataArea });
});
</script>
</body>
</html>
21 changes: 16 additions & 5 deletions src/my-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,12 @@ export class MyMap extends LitElement {
// fit map to extent of geojson features, overriding default zoom & center
fitToData(map, geojsonSource, this.geojsonBuffer);

// log total area of first feature (assumes geojson is a single polygon for now)
// log total area of static geojson data (assumes single polygon for now)
const data = geojsonSource.getFeatures()[0].getGeometry();
console.log("geojsonData total area:", formatArea(data, this.areaUnit));
this.dispatch(
"geojsonDataArea",
formatArea(data, this.areaUnit)
);
}

if (this.drawMode) {
Expand Down Expand Up @@ -314,11 +317,19 @@ export class MyMap extends LitElement {
) {
// fit map to extent of features
fitToData(map, outlineSource, this.featureBuffer);

// write the geojson representation of the feature or merged features
this.dispatch(
"featuresGeojsonChange",
new GeoJSON().writeFeaturesObject(outlineSource.getFeatures(), {
featureProjection: "EPSG:3857",
})
);

// log total area of feature or merged features
// calculate the total area of the feature or merged features
const data = outlineSource.getFeatures()[0].getGeometry();
console.log(
"feature(s) total area:",
this.dispatch(
"featuresAreaChange",
formatArea(data, this.areaUnit)
);
}
Expand Down