Tengo un conjunto de opciones para dibujar, a pesar de que todas funcionan (ya que pertenecen a un código), no puedo dibujar la estrella, que se incorporará desde otra solución.
Lo he recogido de aquí:
https://openlayers.org/en/latest/examples/draw-shapes.html
y luego lo modifiqué según estos enfoques:
https://github.com/openlayers/openlayers/issues/8512
https://dolmenweb.it/viewers/openlayer/examples/draw-shapes.html
Ahora mi código se ve como se puede ver a continuación:
var starInteraction = new ol.interaction.Draw({ source: vectorLayer.getSource(), type: 'Polygon', geometryFunction: function (coordinates, geometry) { if (!geometry) { geometry = new ol.geom.Polygon([newCoordinates]); } else { geometry.setCoordinates([newCoordinates]); } var center = coordinates[0]; var last = coordinates[1]; var dx = center[0] - last[0]; var dy = center[1] - last[1]; var radius = Math.sqrt(dx * dx + dy * dy); var rotation = Math.atan2(dy, dx); var newCoordinates = []; var numPoints = 12; for (var i = 0; i < numPoints; ++i) { var angle = rotation + i * 2 * Math.PI / numPoints; var fraction = i % 2 === 0 ? 1 : 0.5; var offsetX = radius * fraction * Math.cos(angle); var offsetY = radius * fraction * Math.sin(angle); newCoordinates.push([center[0] + offsetX, center[1] + offsetY]); } newCoordinates.push(newCoordinates[0].slice()); geometry.setCoordinates([newCoordinates]); return geometry; } }); starInteraction.setActive(false); starInteraction.on('drawend', onDrawend);Desafortunadamente, estoy recibiendo un error:
TypeError no capturado: no se pueden leer las propiedades de undefined
Se refiere principalmente a esta línea de código:
if (coordinates.length === 0) {que existe en el otro archivo. ¿Qué podría faltar aquí entonces? Mi violín completo se ve así:
No ha definido newCoordinates cuando llama
if (!geometry) { geometry = new ol.geom.Polygon([newCoordinates]); }O cambia esa línea a
if (!geometry) { geometry = new ol.geom.Polygon([]); }por lo que tiene una matriz vacía en lugar de una matriz con una entrada indefinida
Alternativamente, puede mover la línea hacia abajo como en el último ejemplo https://openlayers.org/en/latest/examples/draw-shapes.html
if (!geometry) { geometry = new ol.geom.Polygon([newCoordinates]); } else { geometry.setCoordinates([newCoordinates]); }