3

我是 Openlayers 的新手,非常感谢一些帮助 WMSGetFeatureInfo 工作。我尝试了几个示例,每次将其粘贴到我的代码中时,我都会得到相同的结果:当我单击以获取信息时,我的鼠标只是变成了等待符号。

我决定尝试完全复制这个 OpenLayers 示例:http: //openlayers.org/dev/examples/getfeatureinfo-popup.html

我复制了源代码并将源代码从相对更改为绝对。我让它运行了——除了我有同样的问题!当我从网站运行示例时,我得到了弹出窗口。当我运行我的本地版本(见下文)时,当我点击地图时,我只会得到思考符号。我错过了什么?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<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>GetFeatureInfo Popup</title>

<script src="http://openlayers.org/dev/OpenLayers.js">
</script>
<link rel="stylesheet" href="http://openlayers.org/dev/theme/default/style.css" type="text/css">
<link rel="stylesheet" href="http://openlayers.org/dev/examples/style.css" type="text/css">
<script>
OpenLayers.ProxyHost = "proxy.cgi?url=";

var map, info;

function load() {
    map = new OpenLayers.Map({
        div: "map",
        maxExtent: new OpenLayers.Bounds(143.834,-43.648,148.479,-39.573)
    });

    var political = new OpenLayers.Layer.WMS("State Boundaries",
        "http://demo.opengeo.org/geoserver/wms", 
        {'layers': 'topp:tasmania_state_boundaries', transparent: true, format: 'image/gif'},
        {isBaseLayer: true}
    );

    var roads = new OpenLayers.Layer.WMS("Roads",
        "http://demo.opengeo.org/geoserver/wms", 
        {'layers': 'topp:tasmania_roads', transparent: true, format: 'image/gif'},
        {isBaseLayer: false}
    );

    var cities = new OpenLayers.Layer.WMS("Cities",
        "http://demo.opengeo.org/geoserver/wms", 
        {'layers': 'topp:tasmania_cities', transparent: true, format: 'image/gif'},
        {isBaseLayer: false}
    );

    var water = new OpenLayers.Layer.WMS("Bodies of Water",
        "http://demo.opengeo.org/geoserver/wms", 
        {'layers': 'topp:tasmania_water_bodies', transparent: true, format: 'image/gif'},
        {isBaseLayer: false}
    );

    var highlight = new OpenLayers.Layer.Vector("Highlighted Features", {
        displayInLayerSwitcher: false, 
        isBaseLayer: false 
    });

    map.addLayers([political, roads, cities, water, highlight]); 

    info = new OpenLayers.Control.WMSGetFeatureInfo({
        url: 'http://demo.opengeo.org/geoserver/wms', 
        title: 'Identify features by clicking',
        queryVisible: true,
        eventListeners: {
            getfeatureinfo: function(event) {
                map.addPopup(new OpenLayers.Popup.FramedCloud(
                    "chicken", 
                    map.getLonLatFromPixel(event.xy),
                    null,
                    event.text,
                    null,
                    true
                ));
            }
        }
    });
    map.addControl(info);
    info.activate();

    map.addControl(new OpenLayers.Control.LayerSwitcher());
    map.zoomToMaxExtent();
}

</script>
</head>
<body onload="load()">
<h1 id="title">Feature Info in Popup</h1>
<div id="tags">WMS, GetFeatureInfo, popup</div>
<p id="shortdesc">Demonstrates the WMSGetFeatureInfo control for fetching information about a position from WMS (via GetFeatureInfo request). Results are displayed in a popup.       </p>
<div id="map" class="smallmap"></div>
<div id="docs"></div>
</body>
</html>
4

2 回答 2

3

这听起来像是跨域请求的问题。对于 WMS getMap,请求是针对图像的,因此可以使用任何域。对于 GetFeatureInfo,请求是针对 XML 的,因此必须针对同一个域。

为了克服这个问题,OpenLayers 中有一个代理设置,这应该会有所帮助

基本上,您必须将设置OpenLayers.ProxyHost设置为指向代理 cgi 脚本,OpenLayers src 中提供了该脚本的示例。

于 2012-11-17T16:01:29.853 回答
1

我弄清楚我的例子做错了什么。我将 SCRIPT 和 LINK 引用从相对更改为绝对,但未更改以下内容:

OpenLayers.ProxyHost = "proxy.cgi?url=";

当我将其更改为:

OpenLayers.ProxyHost = "http://openlayers.org/dev/examples/proxy.cgi?url=";

该示例在我的 PC 上运行良好。

于 2012-11-19T19:26:40.227 回答