Talk:OpenLayers Simple Example
Discuss OpenLayers Simple Example
including in <table>
I have tried the example and it worked out of the box, however when I tried to integrate the map in a html-table, it didn't render anymore. This is somewhat confusing because when I view the html in firebug, apparently init() worked properly and filled the div. Can anybody tell me what the cause of this is?
Thanks Brndnbg 21:48, 15 November 2009 (UTC)
It would perhaps be a good idea to add the
lonLatToMercator() function (from the OSM site here: ) to the example, parse the default lat/lon into with this function so that it is obvious what is happening at this step.
It would probably help to define the initial lat/lon as an OpenLayers lat/lon object, too. --Thomas Wood 19:22, 17 October 2007 (BST)
These days that's all nicely simplified by the provision of 'tranform' methods, as illustrated on the page now. There's also a nice document explaining projections in an OpenLayers context. At the moment I'm looking at the content and thinking we could farm off the proj4 stuff onto a separate page -- Harry Wood 12:31, 6 August 2010 (BST)
Reverted non-simple browser detecting and code wrapping stuff
But generally I would say it definitely doesn't fit the criteria of providing a simple example. One nice thing about keeping it simple, is that if a developer can understand it, then they'll be able to figure out the necessary shoehorning into existing pages anyway.
-- Harry Wood 17:18, 11 December 2007 (UTC)
Adding segments (lines) on an OpenLayers map?
So, this is the coolest thing since sliced butter. I've seen the Openlayers POI layer example for how I can add points of interest, but is there any way I can add segments (lines between points) on an OSM map? In particular, I'd like to graphically display flight routes (London-New York, etc) between arbitrary lat/lon points. Just a simple straight line would be fine for starters, but for extra credit, geometrically accurate curved line projections would be even nicer. Jpatokal 12:44, 18 July 2008 (UTC)
- Reply to self: apparently PointTracks do pretty much what I need. Jpatokal 12:48, 18 July 2008 (UTC)
- hmmm. Haven't tried it, but I was playing with the OpenLayers examples, and noticed there's one which lets the user draw lines onto the map: http://openlayers.org/dev/examples/drag-feature.html ...Positioning some static lines should be less complex than that I guess. -- Harry Wood 13:23, 18 July 2008 (UTC)
- You can also render many common geographic files such as KML, GML, GeoRSS etc through use of the GML layer and the appropriate Format object. The API docs contain more info. --Thomas Wood 17:01, 18 July 2008 (UTC)
These days a lot of stuff can be done with a 'Vector' layer. simple Vector Linestring example. That's kind of the raw way of doing it, with more flexibility than reading KML/GML/GeoRSS in. -- Harry Wood 12:31, 6 August 2010 (BST)
Even the simple example should include the license notice. Lulu-Ann
- It does
- When someone uses the OpenLayers.Layer.OSM layer class as in this example, OpenLayers will display the attribution text. This is defined in Layer/XYZ.js and currently reads "Data CC-BY-SA OpenStreetMap"
- -- Harry Wood 16:19, 30 June 2010 (UTC)
- OK then :-)
Needs updating for ol.js 5.3.0
I tried using this example for placing a marker and it did not work. I have modified it to use the latest naming for the library to ol.js and tried fixing the calls for the new methods. But, I am getting an error. Here's my verions of the add markers example:
<!DOCTYPE HTML> <html> <head> <title>OpenLayers Simplest Example</title> </head> <body>
<script src="./build/ol.js"></script> <script> var lat = 47.35387; var lon = 8.43609; var zoom = 18; var fromProjection = new ol.proj.Projection("EPSG:4326"); // Transform from WGS 1984 var toProjection = new ol.proj.Projection("EPSG:900913"); // to Spherical Mercator Projection var position = new ol.proj.toLonLat(lon, lat).transform( fromProjection, toProjection); map = new ol.Map("Map"); var mapnik = new ol.Layer.OSM(); map.addLayer(mapnik); var markers = new ol.Layer.Markers( "Markers" ); map.addLayer(markers); markers.addMarker(new ol.Marker(position)); map.setCenter(position, zoom); </script> </body> </html>
But I get this error in my Chrome console:
Uncaught TypeError: t.getCode is not a function at Le (proj.js:419) at Oe (proj.js:442) at Pe (proj.js:461) at new ux.proj.toLonLat (proj.js:375) at index.html:16
Minimal Working Example for OpenLayers 6.1.1
I have adapted OpenLayer's Quick Start example code for a 2019 edition of the OpenLayers Simple Example. To use it,
- Get hold of the necessary code:
- Create a new file
index.htmlin the same directory as the two OpenLayer files.
- Add code to it, see below.
index.htmlin your browser.
- You should see a simple map titled "My Map".