ol-ext/examples/map.control.bar.html
2016-11-06 20:15:32 +01:00

332 lines
11 KiB
HTML

<!DOCTYPE html>
<!----------------------------------------------------------
Copyright (c) 2015 Jean-Marc VIGLINO,
released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
------------------------------------------------------------>
<html>
<head>
<title>OL3-ext: Control bar</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="ol.control.Bar is a control bar that contains controls." />
<meta name="keywords" content="ol3, control, bar, panel, ol3, openlayers, interaction" />
<link rel="stylesheet" href="style.css" />
<!-- jQuery -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<!-- FontAwesome -->
<link rel="stylesheet" href="http://fontawesome.io/assets/font-awesome/css/font-awesome.css" type="text/css" />
<!-- OL3 -->
<link rel="stylesheet" href="http://openlayers.org/en/master/css/ol.css" />
<script type="text/javascript" src="http://openlayers.org/en/master/build/ol.js"></script>
<!-- controls -->
<link rel="stylesheet" href="../control/controlbar.css" type="text/css" />
<script type="text/javascript" src="../control/controlbar.js"></script>
<script type="text/javascript" src="../control/buttoncontrol.js"></script>
<script type="text/javascript" src="../control/togglecontrol.js"></script>
<style>
.ol-button i
{ color: inherit;
}
</style>
</head>
<body >
<a href="https://github.com/Viglino/ol3-ext"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a>
<a href="../index.html">
<h1>OL3-ext: control bar</h1>
</a>
<div class="info">
<i>ol.control.Bar</i> is a panel that contains other controls. Control bar can be nested.
<br/>
<i>ol.control.Toogle</i> are customized ol3 controls buttons with an active/deactive position.
If created with an interaction it controls its activation.
<br/>
ol.control.Toogle can be grouped into an ol.control.Bar with a toggleOne propertie to have only one activated at a time.
You can compose toolbars with it.
<br/>
When adding control to a bar, an option bar can be nested to add options visible when the control is active.
</div>
<!-- Map div -->
<div id="map" style="width:600px; height:400px;"></div>
<div class="options" >
<ul>
<li>
Position:
<select onchange="mainbar.setPosition(this.value)">
<option value="top">top</option>
<option value="top-left">top-left</option>
<option value="left">left</option>
<option value="bottom-left">bottom-left</option>
<option value="bottom">bottom</option>
<option value="bottom-right">bottom-right</option>
<option value="right">right</option>
<option value="top-right">top-right</option>
</select>
</li>
</ul>
Export:<br />
<textarea id="export" style="width:25em; height:10em"></textarea>
</div>
<script type="text/javascript">
// Vector layer
var vector = new ol.layer.Vector( { source: new ol.source.Vector() })
// The map
var map = new ol.Map
({ target: 'map',
view: new ol.View
({ zoom: 14,
center: [270701, 6247637]
}),
layers:
[ new ol.layer.Tile({ source: new ol.source.OSM() }),
vector
]
});
// Main control bar
var mainbar = new ol.control.Bar();
map.addControl(mainbar);
// Edit control bar
var editbar = new ol.control.Bar(
{ toggleOne: true, // one control active at the same time
group:false // group controls together
});
mainbar.addControl(editbar);
// Add selection tool (a toggle control with a select interaction)
var selectCtrl = new ol.control.Toggle(
{ html: '<i class="fa fa-hand-pointer-o"></i>',
title: "Select",
interaction: new ol.interaction.Select (),
active:true
});
var sbar = new ol.control.Bar();
sbar.addControl (new ol.control.Toggle(
{ html: '<i class="fa fa-times"></i>',
title: "Delete",
className: "noToggle ol-text-button",
onToggle: function()
{ var features = selectCtrl.getInteraction().getFeatures();
if (!features.getLength()) $("#export").text("Select an object first...");
else $("#export").text(features.getLength()+" object(s) deleted.");
for (var i=0, f; f=features.item(i); i++)
{ vector.getSource().removeFeature(f);
}
selectCtrl.getInteraction().getFeatures().clear();
}
}));
sbar.addControl (new ol.control.Toggle(
{ html: '<i class="fa fa-info"></i>',
title: "Show informations",
className: "noToggle ol-text-button",
onToggle: function()
{ switch (selectCtrl.getInteraction().getFeatures().getLength())
{ case 0: $("#export").text("Select an object first...");
break;
case 1:
var f = selectCtrl.getInteraction().getFeatures().item(0);
$("#export").text("Selection is a "+f.getGeometry().getType());
break;
default:
$("#export").text(selectCtrl.getInteraction().getFeatures().getLength()+ " objects seleted.");
break;
}
}
}));
editbar.addControl ( selectCtrl, sbar);
// Add editing tools
var pedit = new ol.control.Toggle(
{ html: '<i class="fa fa-map-marker" ></i>',
title: 'Point',
interaction: new ol.interaction.Draw
({ type: 'Point',
source: vector.getSource()
})
});
editbar.addControl ( pedit );
var ledit = new ol.control.Toggle(
{ html: '<i class="fa fa-share-alt" ></i>',
title: 'LineString',
interaction: new ol.interaction.Draw
({ type: 'LineString',
source: vector.getSource()
})
});
editbar.addControl ( ledit,
// Options bar associated with the control
new ol.control.Bar(
{ controls:[ new ol.control.Toggle(
{ html: 'undo',//'<i class="fa fa-mail-reply"></i>',
title: "Delete last point",
className: "noToggle ol-text-button",
onToggle: function()
{ ledit.getInteraction().removeLastPoint();
}
}),
new ol.control.Toggle(
{ html: 'Finish',
title: "finish",
className: "noToggle ol-text-button",
onToggle: function()
{ // Prevent null objects on finishDrawing
var drawi = ledit.getInteraction();
var lkey = drawi.on('drawend', function(e)
{ drawi.unByKey(lkey);
var c = e.feature.getGeometry().getCoordinates();
if (c.length < 2)
{ throw "Bad LineString";
}
});
try { drawi.finishDrawing(); }
catch(e) { drawi.unByKey(lkey); }
}
})
]
}) );
var fedit = new ol.control.Toggle(
{ html: '<i class="fa fa-bookmark-o fa-rotate-270" ></i>',
title: 'Polygon',
interaction: new ol.interaction.Draw
({ type: 'Polygon',
source: vector.getSource()
})
});
editbar.addControl ( fedit,
// Options bar ssociated with the control
new ol.control.Bar(
{ controls:[ new ol.control.Toggle(
{ html: 'undo',//'<i class="fa fa-mail-reply"></i>',
title: "undo last point",
className: "noToggle ol-text-button",
onToggle: function()
{ fedit.getInteraction().removeLastPoint();
}
}),
new ol.control.Toggle(
{ html: 'finish',
title: "finish",
className: "noToggle ol-text-button",
onToggle: function()
{ // Prevent null objects on finishDrawing
var drawi = fedit.getInteraction();
var lkey = drawi.on('drawend', function(e)
{ drawi.unByKey(lkey);
var c = e.feature.getGeometry().getCoordinates();
if (c[0].length < 4)
{ throw "Bad Polygon";
}
});
try { drawi.finishDrawing(); }
catch(e) { drawi.unByKey(lkey); }
}
})
]
}) );
/*
// Prevent null objects on finishDrawing
vector.getSource().on('addfeature', function(e)
{ switch (e.feature.getGeometry().getType())
{ case 'Polygon':
if (e.feature.getGeometry().getCoordinates()[0].length < 4)
vector.getSource().removeFeature(e.feature);
break;
case 'LineString':
if (e.feature.getGeometry().getCoordinates().length < 2)
vector.getSource().removeFeature(e.feature);
break;
default: break;
}
//console.log(vector.getSource().getFeatures().length)
});
*/
/** Nested bars: add edition in a sub-bar */
// Options bar associated with the control
var subBar = new ol.control.Bar(
{ toggleOne: true,
autoDeactivate: true, // Deactivate control in the subbar when deactivated
controls: [
new ol.control.Toggle(
{ html: '<i class="fa fa-map-marker" ></i>',
title: 'Point',
autoActivate: true,
interaction: new ol.interaction.Draw
({ type: 'Point',
source: vector.getSource()
})
}),
new ol.control.Toggle(
{ html: '<i class="fa fa-share-alt" ></i>',
title: 'Line',
interaction: new ol.interaction.Draw
({ type: 'LineString',
source: vector.getSource()
})
})
]
});
// editbar.addControl ( edit2, subBar );
var edit1 = new ol.control.Toggle(
{ html: '<i class="fa fa-pencil"></i>',
title: "Nested subbar",
});
var edit2 = new ol.control.Toggle(
{ html: '<i class="fa fa-link"></i>',
title: "Nested subbar",
autoActivate: true
});
var subBar1 = new ol.control.Bar({ toggleOne:true, autoDeactivate: true });
editbar.addControl ( edit1, subBar1 );
subBar1.addControl ( edit2, subBar );
subBar1.addControl ( new ol.control.Toggle(
{ html: '<i class="fa fa-ban"></i>',
title: "Nested subbar",
}) );
/* end nested bars */
// Add a custom push button with onToggle function
mainbar.addControl ( new ol.control.Toggle(
{ html: '<i class="fa fa-smile-o"></i>',
title: "Hello world!",
onToggle: function(active)
{ if (active) alert("Hello, I'm active");
else alert("Hello, I'm not active");
}
}));
// Add a save button with on active event
var save = new ol.control.Toggle(
{ html: '<i class="fa fa-download"></i>',
title: "Save",
className: "noToggle"
});
mainbar.addControl ( save );
save.on("change:active", function(e)
{ var json= new ol.format.GeoJSON().writeFeatures(vector.getSource().getFeatures());
$("#export").text(json);
});
</script>
</body>
</html>