GeoJSON + OpenLayers + OSM

时间:2013-08-20 09:02:52

标签: openlayers openstreetmap geojson

这是一个基本的例子,稍微从OpenLayers网站修改过。

  • 当我使用WMS(r2注释,r3取消注释)时,它可以工作。
  • 当我使用OSM(r2取消注释,r2注释)时,它无法正常工作。

我想使用OSM,我在这里缺少什么?

        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));

此代码段代码可以在html页面中使用:

<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>

1 个答案:

答案 0 :(得分:0)

对于OSM,您需要添加投影内容(在您的示例中,您将lon / lat与OSM混合):

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

例如: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>

如果你需要添加谷歌地图(mercator),你还需要看一下这些:

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

希望这有帮助,