Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lydemann
Created October 21, 2018 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lydemann/58fda6130e05bccc9b034c3eae7392c7 to your computer and use it in GitHub Desktop.
Save lydemann/58fda6130e05bccc9b034c3eae7392c7 to your computer and use it in GitHub Desktop.
@Component({
selector: 'app-draw-controls',
templateUrl: './draw-controls.component.html',
styleUrls: ['./draw-controls.component.scss'],
})
export class DrawControlsComponent implements OnInit {
drawInteraction: interaction.Draw;
selectedDrawType: 'Point' | 'Polygon';
constructor(private map: MapComponent, private mapSource: SourceVectorComponent) {
}
ngOnInit() {
}
drawPointSelected() {
this.removeDrawInteraction();
if (this.selectedDrawType === 'Point') {
this.selectedDrawType = null;
return;
}
const mapSource = this.mapSource.instance;
this.drawInteraction = new interaction.Draw({ type: 'Point', source: mapSource });
this.drawInteraction.on('drawend', (event) => this.endDrawing(event));
this.map.instance.addInteraction(this.drawInteraction);
this.selectedDrawType = 'Point';
}
drawPolygonSelected() {
this.removeDrawInteraction();
if (this.selectedDrawType === 'Polygon') {
this.selectedDrawType = null;
return;
}
const mapSource = this.mapSource.instance;
this.drawInteraction = new interaction.Draw({ type: 'Polygon', source: mapSource });
this.drawInteraction.on('drawend', (event) => this.endDrawing(event));
this.map.instance.addInteraction(this.drawInteraction);
this.selectedDrawType = 'Polygon';
}
private removeDrawInteraction() {
if (this.drawInteraction) {
this.map.instance.removeInteraction(this.drawInteraction);
}
}
endDrawing(feat: any) {
const polygon = feat.feature.getGeometry() as geom.Polygon;
console.log(polygon.getCoordinates());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment