Commit 20569ccb authored by Adrien Pavie's avatar Adrien Pavie
Browse files

Auto-zoom on shapes when no initial map position is provided

parent 8bb1cd65
......@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## Next version (to be released)
### Added
- If no map position is provided when loading an existing sketch, it zooms automatically to existing shapes
## 0.1.1
......
......@@ -27,23 +27,27 @@ exports.createSketch = options => {
* @param {string} sid The sketch ID
* @return {Promise} Resolves on GeoJSON containing data from sketch
*/
exports.loadSketch = sid => {
exports.loadSketch = (sid) => {
return pool.query("SELECT options FROM sketch WHERE id = $1", [ sid ])
.then(result => {
if(result.rows.length === 1) {
const geojson = { type: "FeatureCollection", properties: result.rows[0].options };
return pool.query(`
SELECT COALESCE(array_agg(json_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(geom)::json,
'properties', properties,
'id', id
)), ARRAY[]::json[]) AS features
SELECT
COALESCE(array_agg(json_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(geom)::json,
'properties', properties,
'id', id
)), ARRAY[]::json[]) AS features,
ST_AsGeoJSON(ST_Envelope(ST_Collect(geom)))::json AS envelope
FROM shape
WHERE sketch = $1
`, [ sid ])
.then(result2 => {
geojson.features = result2.rows[0].features;
const bbox = result2.rows[0].envelope.coordinates[0];
geojson.bbox = [ bbox[0][0], bbox[0][1], bbox[2][0], bbox[2][1] ];
return geojson;
});
}
......
......@@ -12,7 +12,7 @@ const COLORS = [
// Script-wide variables
let map, sid, shapesLayer, tmpShapeCoordinates,
tmpShapeLayer, timerGeocoder;
tmpShapeLayer, timerGeocoder, noStartLocation;
const drawOpts = {
color: "red",
penSize: 20,
......@@ -45,6 +45,9 @@ function loadSketch(id) {
socket.on("sketch_loaded", geojson => {
sid = id;
if(noStartLocation) {
map.fitBounds([ [ geojson.bbox[1], geojson.bbox[0] ], [ geojson.bbox[3], geojson.bbox[2] ] ]);
}
showLoader(false);
showShapes(geojson);
listenAPIChanges();
......@@ -455,8 +458,10 @@ function init() {
map.attributionControl.setPrefix(null);
map.addHash();
noStartLocation = location.hash === "#1/0/0";
if(location.hash.length === 0 || !/^#\d+\/-?\d+(\.\d+)?\/-?\d+(\.\d+)?$/.test(location.hash)) {
map.setView([0,0], 1);
noStartLocation = true;
}
const tiles = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment