diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-02-04 12:46:49 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-04-24 19:38:56 +0200 |
commit | 7db3c03eed446df0f2dfb84dec9467cf43589e63 (patch) | |
tree | d7cf1f8c7d31f3a8a51d2c532c6b6ba82f440280 /ishtar_common/static/js/ishtar-map.js | |
parent | b665a5ecec08e057e78320c43c702ebefb5a7a29 (diff) | |
download | Ishtar-7db3c03eed446df0f2dfb84dec9467cf43589e63.tar.bz2 Ishtar-7db3c03eed446df0f2dfb84dec9467cf43589e63.zip |
Map: manage default extent and zoom to current extent
Diffstat (limited to 'ishtar_common/static/js/ishtar-map.js')
-rw-r--r-- | ishtar_common/static/js/ishtar-map.js | 64 |
1 files changed, 52 insertions, 12 deletions
diff --git a/ishtar_common/static/js/ishtar-map.js b/ishtar_common/static/js/ishtar-map.js index a8e1a4fa5..ce8f9b616 100644 --- a/ishtar_common/static/js/ishtar-map.js +++ b/ishtar_common/static/js/ishtar-map.js @@ -4,6 +4,9 @@ var default_pointer = "../media/images/default-pointer.png"; var view_projection = 'EPSG:3857'; +var map_default_center = 'SRID=4326;POINT (2.4397 46.5528)'; +var map_default_zoom = '7'; + var source_osm = function(options){ return new ol.layer.Tile({ source: new ol.source.OSM() @@ -11,7 +14,7 @@ var source_osm = function(options){ }; -var map_layers = { +var default_map_layers = { 'osm': source_osm }; @@ -23,7 +26,7 @@ var get_layers = function(layers){ for (idx in layers){ var layer_attr = layers[idx]; ol_layers.push( - map_layers[layer_attr['type']](layer_attr['options']) + default_map_layers[layer_attr['type']](layer_attr['options']) ); } return ol_layers; @@ -67,17 +70,25 @@ var get_style = function(feature){ /* display map */ var vector_source; var vector_layer; -var map_layers; +var center; var map; -var geojson_format = new ol.format.GeoJSON( - {dataProjection:'EPSG:4326', featureProjection: view_projection}); +var map_view; +var map_layers; +var proj_options = { + dataProjection:'EPSG:4326', featureProjection: view_projection +} +var geojson_format = new ol.format.GeoJSON(proj_options); +var wkt_format = new ol.format.WKT(proj_options); +var initialize_map = function(map_id, layers){ + center = wkt_format.readGeometry(map_default_center).getCoordinates(); -var display_map = function(map_id, points, layers){ + /* vector_source = new ol.source.Vector({ features: geojson_format.readFeatures(points) }); - + */ + vector_source = new ol.source.Vector(); vector_layer = new ol.layer.Vector({ source: vector_source, style: get_style @@ -85,15 +96,44 @@ var display_map = function(map_id, points, layers){ map_layers = get_layers(layers); map_layers.push(vector_layer); + + map_view = new ol.View({ + projection: view_projection, + center: ol.proj.fromLonLat([center[0], center[1]]), + zoom: map_default_zoom + }); + map = new ol.Map({ target: map_id, layers: map_layers, - view: new ol.View({ - projection: view_projection, - center: ol.proj.fromLonLat([37.41, 8.82]), - zoom: 4 - }) + view: map_view }); } +var redraw_map = function(map_id, layers){ + map.setTarget(null); + map = null; + initialize_map(map_id, layers); +}; + + +var display_map = function(map_id, points, layers){ + if (map){ + redraw_map(map_id, layers); + } else { + initialize_map(map_id, layers); + } + vector_source.clear(); + vector_source.addFeatures(geojson_format.readFeatures(points)); + + map.updateSize(); + if (points.features.length){ + map_view.fit(vector_source.getExtent()); + if (map_view.getZoom() > 12){ + map_view.setZoom(12); + } + } + +} + |