Search code examples
openlayersopenstreetmapgeojson

GeoJSON + OpenLayers + OSM


This is a basic example, slightly modified from the OpenLayers website.

  • When I use WMS (r2 commented, r3 uncommented) it works.
  • When I use OSM (r2 uncommented, r2 commented) it wont work.

I want to use OSM, what am I missing here?

        var map = new OpenLayers.Map('map');
        //osmLayer = new OpenLayers.Layer.OSM();
        osmLayer = new OpenLayers.Layer.WMS("OpenLayers WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'});
        map.addLayer(osmLayer);
        map.setCenter(new OpenLayers.LonLat(10, 45), 6);

        var myGeoJSON = {
            "type": "FeatureCollection",
            "features": [
                {"geometry":{
                        "type":"GeometryCollection",
                        "geometries":[
                            {
                                "type":"LineString",
                                "coordinates":
                                    [[11.0878902207, 45.1602390564],
                                     [15.01953125, 48.1298828125]]
                             }
                        ]
                    },
                    "type":"Feature",
                    "properties": {}}                        
                ]
        };

        var geojson_format = new OpenLayers.Format.GeoJSON();
        var vector_layer = new OpenLayers.Layer.Vector();
        map.addLayer(vector_layer);
        vector_layer.addFeatures(geojson_format.read(myGeoJSON));

This snippet code can be used in a html page with:

<script src="http://openlayers.org/api/OpenLayers.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>

<div style="width: 100%; height: 100%;" id="map"></div>

Solution

  • For OSM you need to add projection stuff (in your example you're mixing lon/lat with OSM):

    http://wiki.openstreetmap.org/wiki/EPSG:3857

    such as here: http://openlayers.org/dev/examples/osm.html

    <!DOCTYPE html>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <title>OpenLayers Basic OSM Example</title>
        <link rel="stylesheet" href="../theme/default/style.css" type="text/css">
        <link rel="stylesheet" href="style.css" type="text/css">
        <script src="../OpenLayers.js"></script>
        <script type="text/javascript">
            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
                );    
            }
        </script>
      </head>
      <body onload="init()">
        <h1 id="title">Basic OSM Example</h1>
    
        <div id="tags">
            openstreetmap basic light
        </div>
    
        <div id="shortdesc">Show a Simple OSM Map</div>
    
        <div id="map" class="smallmap"></div>
    
        <div id="docs">
            <p>This example shows a very simple OSM layout with minimal controls.</p> 
        </div>
      </body>
    </html>
    

    if you need to add google maps (mercator), you'd need to take a look at these ones also:

    https://gis.stackexchange.com/questions/17231/openlayers-and-geoserver-osm-google-maps-and-wms-overlay OSM: convert ties from projected coordinates in spherical mercator "EPSG:900913" to "EPSG:4326" coordinates http://docs.openlayers.org/library/spherical_mercator.html

    Hope this helps,