use of ol.layer.VectorLayerOptions in project gwt-ol3 by TDesjardins.
the class GeoJsonExample method show.
/* (non-Javadoc)
* @see de.desjardins.ol3.demo.client.example.Example#show() */
@Override
public void show(String exampleId) {
// create linestring
Coordinate coordinate1 = new Coordinate(4e6, 2e6);
Coordinate coordinate2 = new Coordinate(8e6, -2e6);
Coordinate[] coordinates = { coordinate1, coordinate2 };
LineString lineString = new LineString(coordinates);
// create feature
FeatureOptions featureOptions = new FeatureOptions();
featureOptions.setGeometry(lineString);
Feature feature = new Feature(featureOptions);
// convert feature to GeoJSON
GeoJson geoJsonFormat = new GeoJson();
java.lang.Object geoJson = geoJsonFormat.writeFeatureObject(feature, null);
// convert features from GeoJSON
Feature featureGeoJson = geoJsonFormat.readFeature(geoJson, null);
// show converted features
Collection<Feature> lstFeatures = new Collection<Feature>();
lstFeatures.push(featureGeoJson);
VectorOptions vectorSourceOptions = new VectorOptions();
vectorSourceOptions.setFeatures(lstFeatures);
Vector vectorSource = new Vector(vectorSourceOptions);
VectorLayerOptions vectorLayerOptions = new VectorLayerOptions();
vectorLayerOptions.setSource(vectorSource);
ol.layer.Vector vectorLayer = new ol.layer.Vector(vectorLayerOptions);
// create a OSM-layer
XyzOptions osmSourceOptions = new XyzOptions();
Osm osmSource = new Osm(osmSourceOptions);
LayerOptions osmLayerOptions = new LayerOptions();
osmLayerOptions.setSource(osmSource);
Tile osmLayer = new Tile(osmLayerOptions);
// create a view
View view = new View();
Coordinate centerCoordinate = new Coordinate(0, 0);
view.setCenter(centerCoordinate);
view.setZoom(2);
// create the map
MapOptions mapOptions = new MapOptions();
mapOptions.setTarget(exampleId);
mapOptions.setView(view);
Collection<Base> lstLayer = new Collection<Base>();
lstLayer.push(osmLayer);
lstLayer.push(vectorLayer);
mapOptions.setLayers(lstLayer);
Map map = new Map(mapOptions);
// add some controls
map.addControl(new ScaleLine());
DemoUtils.addDefaultControls(map.getControls());
// add some interactions
map.addInteraction(new KeyboardPan());
map.addInteraction(new KeyboardZoom());
map.addControl(new Rotate());
}
use of ol.layer.VectorLayerOptions in project gwt-ol3 by TDesjardins.
the class MarkerExample method show.
/* (non-Javadoc)
* @see de.desjardins.ol3.demo.client.example.Example#show() */
@Override
public void show(String exampleId) {
// create a point
Coordinate coordinate1 = OLFactory.createCoordinate(4e6, 2e6);
Point point1 = new Point(coordinate1);
// create feature
FeatureOptions featureOptions = OLFactory.createOptions();
featureOptions.setGeometry(point1);
Feature feature = new Feature(featureOptions);
Collection<Feature> features = new Collection<Feature>();
features.push(feature);
// create source
VectorOptions vectorSourceOptions = OLFactory.createOptions();
vectorSourceOptions.setFeatures(features);
Vector vectorSource = new Vector(vectorSourceOptions);
// create style
StyleOptions styleOptions = new StyleOptions();
IconOptions iconOptions = new IconOptions();
iconOptions.setSrc("https://openlayers.org/en/v3.20.1/examples/data/icon.png");
Icon icon = new Icon(iconOptions);
styleOptions.setImage(icon);
Style style = new Style(styleOptions);
VectorLayerOptions vectorLayerOptions = OLFactory.createOptions();
vectorLayerOptions.setSource(vectorSource);
vectorLayerOptions.setStyle(style);
ol.layer.Vector vectorLayer = new ol.layer.Vector(vectorLayerOptions);
// create a OSM-layer
XyzOptions osmSourceOptions = OLFactory.createOptions();
Osm osmSource = new Osm(osmSourceOptions);
LayerOptions osmLayerOptions = OLFactory.createOptions();
osmLayerOptions.setSource(osmSource);
Tile osmLayer = new Tile(osmLayerOptions);
// create a view
View view = new View();
Coordinate centerCoordinate = OLFactory.createCoordinate(0, 0);
view.setCenter(centerCoordinate);
view.setZoom(2);
// create the map
MapOptions mapOptions = new MapOptions();
mapOptions.setTarget(exampleId);
mapOptions.setView(view);
Collection<Base> lstLayer = new Collection<Base>();
lstLayer.push(osmLayer);
lstLayer.push(vectorLayer);
mapOptions.setLayers(lstLayer);
Map map = new Map(mapOptions);
// add some controls
map.addControl(new ScaleLine());
DemoUtils.addDefaultControls(map.getControls());
// add some interactions
map.addInteraction(new KeyboardPan());
map.addInteraction(new KeyboardZoom());
map.addControl(new Rotate());
}
use of ol.layer.VectorLayerOptions in project gwt-ol3 by TDesjardins.
the class SelectFeaturesExample method show.
/* (non-Javadoc)
* @see de.desjardins.ol3.demo.client.example.Example#show() */
@Override
public void show(String exampleId) {
Coordinate centerCoordinate = new Coordinate(13.37, 52.52);
Coordinate transformedMidPoint = Projection.transform(centerCoordinate, DemoConstants.EPSG_4326, DemoConstants.EPSG_3857);
// create a polygon
Polygon polygon = DemoUtils.createTestPolygon();
// create a feature
FeatureOptions featureOptions = OLFactory.createOptions();
// TODO Setter for ID seems to doesn't have an effect in feature options.
// featureOptions.setId("g1");
featureOptions.setGeometry(polygon);
Feature feature = new Feature(featureOptions);
feature.setId("g1");
feature.set("name", "triangle");
// create another feature via cloning
Feature feature2 = feature.clone();
feature2.setId("g2");
feature2.getGeometry().rotate(180, transformedMidPoint);
Collection<Feature> lstFeatures = new Collection<Feature>();
lstFeatures.push(feature);
lstFeatures.push(feature2);
VectorOptions vectorSourceOptions = OLFactory.createOptions();
vectorSourceOptions.setFeatures(lstFeatures);
Vector vectorSource = new Vector(vectorSourceOptions);
VectorLayerOptions vectorLayerOptions = OLFactory.createOptions();
vectorLayerOptions.setSource(vectorSource);
ol.layer.Vector vectorLayer = new ol.layer.Vector(vectorLayerOptions);
// create an OSM-layer
XyzOptions osmSourceOptions = OLFactory.createOptions();
Osm osmSource = new Osm(osmSourceOptions);
LayerOptions osmLayerOptions = OLFactory.createOptions();
osmLayerOptions.setSource(osmSource);
Tile osmLayer = new Tile(osmLayerOptions);
// create a view
View view = new View();
view.setCenter(transformedMidPoint);
view.setZoom(14);
// create the map
MapOptions mapOptions = OLFactory.createOptions();
mapOptions.setTarget(exampleId);
mapOptions.setView(view);
Collection<Base> lstLayer = new Collection<Base>();
lstLayer.push(osmLayer);
lstLayer.push(vectorLayer);
mapOptions.setLayers(lstLayer);
Map map = new Map(mapOptions);
// add some controls
map.addControl(new ScaleLine());
MousePositionOptions mousePositionOptions = OLFactory.createOptions();
ProjectionOptions projectionOptions = OLFactory.createOptions();
projectionOptions.setCode(DemoConstants.EPSG_4326);
mousePositionOptions.setProjection(new Projection(projectionOptions));
MousePosition mousePosition = new MousePosition(mousePositionOptions);
mousePosition.setCoordinateFormat(Coordinate.createStringXY(5));
map.addControl(mousePosition);
SelectOptions selectOptions = new SelectOptions();
selectOptions.setCondition(Condition.getClick());
// create a select interaction
final Select selectFeature = new Select(selectOptions);
map.addInteraction(selectFeature);
selectFeature.on("select", (event) -> {
Collection<Feature> selectedFeatures = selectFeature.getFeatures();
if (selectedFeatures.getLength() > 0) {
Feature selectedFeature = selectedFeatures.item(0);
String output = "You selected feature with id '" + selectedFeature.getId() + "'" + " and name '" + selectedFeature.get("name") + "'" + " and geometry name '" + selectedFeature.getGeometryName() + "'" + ".";
Window.alert(output);
}
});
}
use of ol.layer.VectorLayerOptions in project gwt-ol3 by TDesjardins.
the class OLFactory method createLayerOptionsWithSource.
/**
* Creates {@link VectorLayerOptions} using the given
* {@link ol.source.Vector}.
*
* @param source
* {@link ol.source.Vector}
* @return {@link VectorLayerOptions}
*/
public static VectorLayerOptions createLayerOptionsWithSource(ol.source.Vector source) {
VectorLayerOptions options = createOptions();
options.setSource(source);
return options;
}
use of ol.layer.VectorLayerOptions 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;
}
Aggregations