Skip to content

Instantly share code, notes, and snippets.

@manuelep
Last active June 24, 2017 06:22
Show Gist options
  • Save manuelep/57636f8d72d7004a18a7b06a34b91f61 to your computer and use it in GitHub Desktop.
Save manuelep/57636f8d72d7004a18a7b06a34b91f61 to your computer and use it in GitHub Desktop.
A web service that draw a map to be called using LOAD web2py helper
@service.run
def getmap(conn, tablename, the_geom="geom", bbox=''):
""" """
session.forget(response)
js = """
var url = "%(url)s";
var gargs = "%(bbox)s" && '?bbox='+"%(bbox)s";
var dynSource = new ol.source.Vector({
url: url + gargs,
format: new ol.format.GeoJSON(),
loader: function(extent, resolution, projection) {
$.ajax({
url: url + '?bbox=' + extent.join(','),
success: function(data) {
if (data.features.length>0) {
dynSource.addFeatures(dynSource.getFormat().readFeatures(data));
}
}
});
},
projection: 'EPSG:3857',
strategy: ol.loadingstrategy.bbox
});
var map = new ol.Map({
"target": "map",
"layers": [
new ol.layer.Tile({opacity: 0.3, source: new ol.source.OSM()}),
new ol.layer.Vector({"source": dynSource})
],
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
function fitmap (mymap) {
if ( Number.isFinite(dynSource.getExtent()[0])!==false ) {
mymap.getView().fit(dynSource.getExtent(), mymap.getSize())
} else {
setTimeout(function () {fitmap(mymap)}, 500)
};
}
fitmap(map);
""" % dict(
url = URL("default", "call", args=("run", "geom", conn, tablename, the_geom,)),
bbox = bbox
)
response.js = jsmin(js)
return DIV(_id="map", _style="height: 500px;")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment