1

这是一个基本示例,从 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>
4

1 回答 1

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>

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

https://gis.stackexchange.com/questions/17231/openlayers-and-geoserver-osm-google-maps-and-wms-overlay OSM:将球墨卡托投影坐标“EPSG:900913”转换为“EPSG: 4326" 坐标 http://docs.openlayers.org/library/spherical_mercator.html

希望这可以帮助,

于 2013-08-28T13:51:07.270 回答