Openstreetmap: embedding map in webpage (like Google Maps)
You need to use some JavaScript stuff to show your map. OpenLayers is the number one choice for this.
There is an example at http://wiki.openstreetmap.org/wiki/OpenLayers_Simple_Example and something more advanced at
http://wiki.openstreetmap.org/wiki/OpenLayers_Marker
and
http://wiki.openstreetmap.org/wiki/Openlayers_POI_layer_example
There's now also Leaflet, which is built with mobile devices in mind.
There is a Quick Start Guide for leaflet. Besides basic features such as markers, with plugins it also supports routing using an external service.
For a simple map, it is IMHO easier and faster to set up than OpenLayers, yet fully configurable and tweakable for more complex uses.
Simple OSM Slippy Map Demo/Example
Click on "Run code snippet" to see an embedded OpenStreetMap slippy map with a marker on it. This was created with Leaflet.
Code
// Where you want to render the map.
var element = document.getElementById('osm-map');
// Height has to be set. You can do this in CSS too.
element.style = 'height:300px;';
// Create Leaflet map on map element.
var map = L.map(element);
// Add OSM tile layer to the Leaflet map.
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// Target's GPS coordinates.
var target = L.latLng('47.50737', '19.04611');
// Set map's center to target with zoom 14.
map.setView(target, 14);
// Place a marker on the same location.
L.marker(target).addTo(map);
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<link href="https://unpkg.com/[email protected]/dist/leaflet.css" rel="stylesheet"/>
<div id="osm-map"></div>
Specs
- Uses OpenStreetMaps.
- Centers the map to the target GPS.
- Places a marker on the target GPS.
- Only uses Leaflet as a dependency.
Note:
I used the CDN version of Leaflet here, but you can download the files so you can serve and include them from your own host.
If you just want to embed an OSM map on a webpage, the easiest way is to get the iframe code directly from the OSM website:
- Navigate to the map you want on https://www.openstreetmap.org
- On the right side, click the "Share" icon, then click "HTML"
- Copy the resulting iframe code directly into your webpage. It should look like this:
<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"
src="https://www.openstreetmap.org/export/embed.html?bbox=-62.04673002474011%2C16.95487694424327%2C-61.60521696321666%2C17.196751341562923&layer=mapnik"
style="border: 1px solid black"></iframe>
<br/><small><a href="https://www.openstreetmap.org/#map=12/17.0759/-61.8260">View Larger Map</a></small>
If you want to do something more elaborate, see OSM wiki "Deploying your own Slippy Map".