【问题标题】:GeoJSON + OpenLayers + OSMGeoJSON + OpenLayers + OSM
【发布时间】:2013-08-22 06:24:21
【问题描述】:

这是一个基本示例,根据 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));

这个sn-p代码可以用在一个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>

【问题讨论】:

    标签: openlayers openstreetmap geojson


    【解决方案1】:

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

    如果你需要添加谷歌地图(墨卡托),你还需要看看这些:

    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

    希望这会有所帮助,

    【讨论】:

      猜你喜欢
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      • 2022-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      相关资源
      最近更新 更多