我的问题是当我使用像这样的点击事件时:Link 我得到弹出窗口,但我想在此API中显示该弹出窗口中的天气响应: http://api.yr.no/weatherapi/locationforecast/1.8/?lat=60;lon=15 我怎样才能做到这一点?我的代码:
OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
defaultHandlerOptions: {
'single': true,
'double': false,
'pixelTolerance': 0,
'stopSingle': false,
'stopDouble': false
},
initialize: function (options) {
this.handlerOptions = OpenLayers.Util.extend(
{}, this.defaultHandlerOptions
);
OpenLayers.Control.prototype.initialize.apply(
this, arguments
);
this.handler = new OpenLayers.Handler.Click(
this, {
'click': this.trigger
}, this.handlerOptions
);
},
trigger: function (e) {
//A click happened!
var lonlat = map.getLonLatFromViewPortPx(e.xy)
lonlat.transform(
new OpenLayers.Projection("EPSG:900913"),
new OpenLayers.Projection("EPSG:4326")
);
alert("You clicked near " + lonlat.lat + " N, " +
+lonlat.lon + " E");
}
});
OpenLayers.ProxyHost = "../../Proxy/proxy.aspx?url=";
var map;
function initMap(){
map = new OpenLayers.Map("mapCont");
var osm = new OpenLayers.Layer.OSM();
map.addLayer(osm);
var google = new OpenLayers.Layer.Google("GoogleMaps");
map.addLayer(google);
addAdditionalControls();
map.setCenter(new OpenLayers.LonLat(2452105.33, 9780412.11), 15)
addWMSservice();
var click = new OpenLayers.Control.Click();
map.addControl(click);
click.activate();
info = new OpenLayers.Control.WMSGetFeatureInfo({
url: 'http://localhost:8080/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();
}
答案 0 :(得分:0)
这是一个示例函数,它将加载XML并在警报中显示它。
请注意,您的浏览器很可能会遇到访问源问题,因为请求是针对其他域的。
function apiRequest(){
var lat = 60;
var lon = 15;
var url = "http://api.yr.no/weatherapi/locationforecast/1.8/?lat="+lat+";lon="+lon;
$.ajax({
url: url,
type: 'GET',
success: function(data){
alert((new XMLSerializer()).serializeToString(data));
},
error: function(data) {
alert('err:'+data);
}
});
}