OpenLayers Marker Example

From OpenStreetMap Wiki
(Redirected from OpenLayers Marker)
Jump to navigation Jump to search

This is a simple example of adding a marker to an OpenLayers map.


Here is the code that is needed (Copy the following into a new HTML file, and view it in a browser.) :

  <div id="mapdiv"></div>
  <script src=""></script>
    map = new OpenLayers.Map("mapdiv");
    map.addLayer(new OpenLayers.Layer.OSM());

    var lonLat = new OpenLayers.LonLat( -0.1279688 ,51.5077286 )
            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
            map.getProjectionObject() // to Spherical Mercator Projection
    var zoom=16;

    var markers = new OpenLayers.Layer.Markers( "Markers" );
    markers.addMarker(new OpenLayers.Marker(lonLat));
    map.setCenter (lonLat, zoom);

This uses the same lonLat object to place a marker and then to also centre the map. You can add further markers using markers.addMarker(new OpenLayers.Marker(newLonLat)); if you define newLonLat to be another OpenLayers.LonLat object

iframe approach

There is an even simpler HTML fragment to use on your website, using an <iframe> tag. You can easily get the necessary HTML using the Export tab, and choosing the "embeddable HTML" option. The iframe approach is rather limited. If you're interested in trying out additional javascript tricks then the OpenLayers example above is a better starting point.

OpenLayers example with marker and popup

Here is an OpenLayers example with marker and popup. This could be the typical "you can find us here" example...

See also