Add free map your website through this step-by-step tutorial.
No API key needed.
Source Code:
<div id= "map" style= "top: 0; left: 0; bottom: 0; right: 0; position: fixed;" ></div>
Add div #map in your html body.
< script src = " http://www.openlayers.org/api/OpenLayers.js " >< /script>
Add OpenLayers.js javascript library to html document end of [/body].
var lat = 60.230506 ;
var lon = 24.847194
var zoom = 10 ;
Initialize variables.
var fromProjection = new OpenLayers . Projection ( " EPSG:4326 " ); // Transform from WGS 1984
var toProjection = new OpenLayers . Projection ( " EPSG:900913 " ); // to Spherical Mercator Projection
var position = new OpenLayers . LonLat ( lon , lat ). transform ( fromProjection , toProjection );
Transform coordinates EPSG position.
var map = new OpenLayers . Map ( " map " );
Add new OpenLayers map to div by id #map.
var mapnik = new OpenLayers.Layer.OSM();
map.addLayer(mapnik);
Add OpenStreetMap background to layer.
var markers = new OpenLayers . Layer . Markers ( " Markers " );
map . addLayer ( markers );
markers . addMarker ( new OpenLayers . Marker ( position ));
Add new markers to position you created.
map . setCenter ( position , zoom );
And finally center map to your created position.
Whole html file looks like this:
<!DOCTYPE html>
<html>
<head>
<title> OpenLayers Example</title>
<meta charset= "utf-8" />
</head>
<body>
<div id= "map" style= "top: 0; left: 0; bottom: 0; right: 0; position: fixed;" ></div>
<script src= "http://www.openlayers.org/api/OpenLayers.js" ></script>
<script>
var lat = 60.230506 ;
var lon = 24.847194
var zoom = 10 ;
var fromProjection = new OpenLayers . Projection ( " EPSG:4326 " ); // Transform from WGS 1984
var toProjection = new OpenLayers . Projection ( " EPSG:900913 " ); // to Spherical Mercator Projection
var position = new OpenLayers . LonLat ( lon , lat ). transform ( fromProjection , toProjection );
var map = new OpenLayers . Map ( " map " );
// Add OpenStreetMap to layer
var mapnik = new OpenLayers . Layer . OSM ();
map . addLayer ( mapnik );
// Add markers to layer
var markers = new OpenLayers . Layer . Markers ( " Markers " );
map . addLayer ( markers );
markers . addMarker ( new OpenLayers . Marker ( position )); < /p >
map . setCenter ( position , zoom );
</script>
</body>
</html>