首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GeoJSON + OpenLayers + OSM

GeoJSON + OpenLayers + OSM
EN

Stack Overflow用户
提问于 2013-08-20 09:02:51
回答 1查看 1.6K关注 0票数 1

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

  • 当我使用WMS (r2注释,r3未注释)时,它可以工作。
  • 当我使用OSM (r2 un注释,r2注释)时,它将无法工作。

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

代码语言:javascript
复制
        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页面中使用:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-28 13:51:07

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

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

比如这里:http://openlayers.org/dev/examples/osm.html

代码语言:javascript
复制
<!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 mercator.html

希望这能帮上忙

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18331030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档