openlayers java scriptcode and web2py

82 views
Skip to first unread message

Serbulent Ocal

unread,
Feb 19, 2015, 5:54:58 AM2/19/15
to web...@googlegroups.com

I am new in this group.

I am struggling with to put the openlayers java script code to web2py controller (test.py) together. My aim is to combine them and I was wondering

how to put them together to show the tiles as base and the map on top of it?


test.py:

for url in [
'http://dev.openlayers.org/theme/default/style.css',
'http://dev.openlayers.org/examples/style.css',
'http://dev.openlayers.org/OpenLayers.js'
]:
response.files.append(url)

    def map():
mymap = DIV(_id="map", _class="smallmap")

response.js = """var map, layer;
    function init() {
        map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.OSM ("Simple OSM Map");
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(-71.147, 42.472).transform(
                new OpenLayers.Projection("EPSG:4326"),
                map.getProjectionObject()
            ), 12
        );
     };
     init();
 """
return locals()

def index():
mymap = LOAD ('test', 'map.load', ajax=True)
return locals()
openlayers (tile.html)

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  <HTML>
   <HEAD>
  <TITLE>OpenLayers</TITLE>
   <META content="text/html; charset=windows-1252" http-equiv=Content-Type>
  <script src="http://openlayers.org/api/OpenLayers.js"></script>
  <META name=GENERATOR content="MSHTML 9.00.8112.16540"></HEAD>
   <BODY>
     <DIV style="WIDTH: 100%; HEIGHT: 100%" id=map></DIV>
     <SCRIPT type=text/javascript defer>
    var map = new OpenLayers.Map('map');
    var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
        "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );
    var dm_wms = new OpenLayers.Layer.WMS(
        "Canadian Data",
        "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap",
        {
            layers: "bathymetry,land_fn,park,drain_fn,drainage," +
                    "prov_bound,fedlimit,rail,road,popplace",
            transparent: "true",
            format: "image/png"
        },
        {isBaseLayer: false}
    );
    map.addLayers([wms, dm_wms]);
    map.zoomToMaxExtent();
  </SCRIPT>
  </BODY></HTML>

Massimo Di Pierro

unread,
Feb 19, 2015, 11:26:10 PM2/19/15
to web...@googlegroups.com
I am not sure why you are using LOAD and response.js. It seems un-necessary. Also I am not sure it is a good idea to put JS in controllers. Instead I would do:

def index():
      return dict()

def map_data():
      return response.json([.....])

and have an index.html that includes all the JS you need and the calls

jQuery(function(){
   jQuery.getJSON('{{=URL('map_data')}}',function(data_in_json) {
      // display the data_in_json in MAP
   });
});
Reply all
Reply to author
Forward
0 new messages