Search in sources :

Example 1 with DrawOptions

use of ol.interaction.DrawOptions in project gwt-ol3 by TDesjardins.

the class Measure method start.

/**
 * Start measuring using existing interaction.
 *
 * @param type
 *            measure geometry type
 * @param listener
 *            {@link MeasureListener}
 * @param immediate
 *            Fire events on every change to the measured geometry? If false
 *            only one event after finishing is fired. (default is true)
 * @param persist
 *            Keep the temporary measurement sketch drawn after the
 *            measurement is complete. The geometry will persist until a new
 *            measurement is started, the control is deactivated, or
 *            {@link #stop()} is called.
 */
private void start(String type, MeasureListener listener, boolean immediate, boolean persist) {
    // clean up old instance
    stop();
    this.listener = listener;
    // set up interaction
    DrawOptions drawOptions = OLFactory.createOptions();
    drawOptions.setType(type);
    // use a special style?
    if (style != null) {
        drawOptions.setStyle(style);
    }
    draw = OLFactory.createDraw(drawOptions);
    // persist measured features?
    if (persist) {
        // set up overlay options
        VectorLayerOptions voptions = OLFactory.createLayerOptionsWithSource(OLFactory.createVectorSource());
        if (style != null) {
            Style[] styles = new Style[1];
            styles[0] = style;
            voptions.setStyle(styles);
        } else {
            // create a default style resembling the default editing style,
            // but adding a border to polygons
            Style sPoly = OLFactory.createStyle(OLFactory.createFill(OLFactory.createColor(255, 255, 255, 0.5)));
            Style sLine1 = OLFactory.createStyle(OLFactory.createStroke(OLFactory.createColor(255, 255, 255, 1), 5));
            Style sLine2 = OLFactory.createStyle(OLFactory.createStroke(OLFactory.createColor(0, 153, 255, 1), 3));
            // combine all styles
            Style[] s = OLUtil.pushItem(OLUtil.combineStyles(sPoly, sLine1), sLine2);
            voptions.setStyle(s);
        }
        // create an overlay and attach it to the map
        persistOverlay = OLFactory.createVector(voptions);
        persistOverlay.setMap(map);
    }
    // set up projection to be used
    proj = map.getView().getProjection();
    map.addInteraction(draw);
    // set up event handlers
    OLUtil.observe(draw, "drawstart", new EventListener<Draw.Event>() {

        @Override
        public void onEvent(Draw.Event event) {
            // remember measure feature
            sketch = event.getFeature();
            // clean up overlay
            if (persistOverlay != null) {
                persistOverlay.<ol.source.Vector>getSource().clear(false);
            }
        }
    });
    OLUtil.observe(draw, "drawend", new EventListener<Draw.Event>() {

        @Override
        public void onEvent(Draw.Event event) {
            // fire event and clean up
            fireMeasureEvent();
            // persist feature?
            if (persistOverlay != null) {
                persistOverlay.<ol.source.Vector>getSource().addFeature(sketch);
            }
            sketch = null;
        }
    });
    // handle mouse move if immediate updates are requested
    if (immediate) {
        // enable mouse move events on the viewport
        Element elem = map.getViewport();
        com.google.gwt.user.client.Event.sinkEvents(elem, com.google.gwt.user.client.Event.ONMOUSEMOVE);
        // remember old event listener before chaining this listener
        // in-between
        chainedListener = com.google.gwt.user.client.Event.getEventListener(elem);
        com.google.gwt.user.client.Event.setEventListener(elem, new com.google.gwt.user.client.EventListener() {

            @Override
            public void onBrowserEvent(com.google.gwt.user.client.Event event) {
                // check for mouse move events only
                if (event.getType() == "mousemove") {
                    // check if interaction is active and fire event
                    if (draw.getActive()) {
                        fireMeasureEvent();
                    }
                }
                // call chained handler
                if (chainedListener != null) {
                    chainedListener.onBrowserEvent(event);
                }
            }
        });
        eventListenerNeedsCleanup = true;
    }
    // set flag
    isActive = true;
}
Also used : Draw(ol.interaction.Draw) Element(com.google.gwt.dom.client.Element) VectorLayerOptions(ol.layer.VectorLayerOptions) DrawOptions(ol.interaction.DrawOptions) Style(ol.style.Style) MeasureEvent(ol.event.MeasureEvent)

Aggregations

Element (com.google.gwt.dom.client.Element)1 MeasureEvent (ol.event.MeasureEvent)1 Draw (ol.interaction.Draw)1 DrawOptions (ol.interaction.DrawOptions)1 VectorLayerOptions (ol.layer.VectorLayerOptions)1 Style (ol.style.Style)1