添加带有openlayers / geoserver的标记

时间:2012-03-03 02:56:04

标签: openlayers geoserver

我的名字叫托尼。我来自越南。我想在webgis中添加一个带有openlayers / geoserver的标记。这是我的代码:

    <!-- Import OL CSS, auto import does not work with our minified OL.js build -->
     <link rel="stylesheet" type="text/css" href="http://localhost:8080/geoserver/openlayers/theme/default/style.css"/>
    <!-- Basic CSS definitions -->
    <style type="text/css">
    /* General settings */
    body {
        font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
        font-size: small;
    }

    /* The map and the location bar */
    #map {
        clear: both;
        position: relative;
        width: 1350px;
        height: 600px;
        border: 0px solid black;
    }

    </style>

    <!-- Import OpenLayers, reduced, wms read only version -->
    <script src="http://localhost:8080/geoserver/openlayers/OpenLayers.js" type="text/javascript">
    </script>
    <script defer="defer" type="text/javascript">
    var map;

    var tiled;

    // pink tile avoidance
    OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
    // make OL compute scale according to WMS spec
    OpenLayers.DOTS_PER_INCH = 25.4 / 0.28;

    function init(){
        // if this is just a coverage or a group of them, disable a few items,
        // and default to jpeg format
        format = 'image/png';

        var bounds = new OpenLayers.Bounds(
            143.83482400000003, -43.648056,
            148.47914100000003, -39.573891
        );
        var options = {
            controls: [],
            maxExtent: bounds,
            maxResolution: 0.01814186328125,
            projection: "EPSG:4326",
            units: 'degrees'
            };
        map = new OpenLayers.Map('map', options);

        // setup tiled layer
        tiled = new OpenLayers.Layer.WMS(
            "Geoserver layers - Tiled", "http://localhost:8080/geoserver/wms",
            {
                LAYERS: 'tasmania',
                STYLES: '',
                format: format,
                tiled: true,
                tilesOrigin : map.maxExtent.left + ',' + map.maxExtent.bottom
            },
            {
                buffer: 0,
                displayOutsideMaxExtent: true,
                isBaseLayer: true,
                yx : {'EPSG:4326' : true}
            } 
        );

        map.addLayers([tiled]);

        // build up all controls
        map.addControl(new OpenLayers.Control.PanZoomBar({
            position: new OpenLayers.Pixel(2, 15)
        }));
        map.addControl(new OpenLayers.Control.Navigation());
        map.addControl(new OpenLayers.Control.Scale($('scale')));
        map.addControl(new OpenLayers.Control.MousePosition({element: $('location')}));
        map.zoomToExtent(bounds);

        //marker
        var markers = new OpenLayers.Layer.Markers( "Markers" );
        map.addLayer(markers);

        var size = new OpenLayers.Size(21,25);
        var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
        var icon = new OpenLayers.Icon('http://localhost:8080/geoserver/openlayers/img/marker.png', size, offset);
        markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(146.83482400000003, -42.648056),icon));
        markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(146.47914100000003, -41.573891),icon.clone()));

        // support GetFeatureInfo
        map.events.register('click', map, function (e) {
            document.getElementById('nodelist').innerHTML = "Loading... please wait...";
            var params = {
                REQUEST: "GetFeatureInfo",
                EXCEPTIONS: "application/vnd.ogc.se_xml",
                BBOX: map.getExtent().toBBOX(),
                SERVICE: "WMS",
                INFO_FORMAT: 'text/html',
                QUERY_LAYERS: map.layers[0].params.LAYERS,
                FEATURE_COUNT: 50,
                Layers: 'tasmania',
                WIDTH: map.size.w,
                HEIGHT: map.size.h,
                format: format,
                styles: map.layers[0].params.STYLES,
                srs: map.layers[0].params.SRS};

            OpenLayers.loadURL("http://localhost:8080/geoserver/wms", params, this, setHTML, setHTML);
            OpenLayers.Event.stop(e);
        });
    }


    </script>
</head>
<body onload="init()">
<h1 id="title">Markers Layer Example</h1>
<div id="map"> 
</div>

</body>

我遇到的问题是它不会显示标记,只显示网页,我不知道为什么。这是我的webmap的样子:

---- HTTP://i.stack.imgur.com/OWZtP.jpg---

我的图片路径为真http://i1133.photobucket.com/albums/m591/tvquang_dn/marker.jpg

我按照这个页面

http://dev.openlayers.org/releases/OpenLayers-2.7/doc/apidocs/files/OpenLayers/Marker-js.html编写我的代码。 非常感谢您的阅读和帮助。

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。 因为默认情况下openlayer.js文件存在于geoserver中是171 kb,这对于大多数openlayers操作没有帮助,比如标记弹出等...因此你需要得到一个完美的OpenlLayer.js文件,大约300kb以上来自openlayer网站。

您的代码完美无需更改任何内容。 只需更改脚本源 目前正在

      

它肯定会起作用.. 问候, 拉夫