we are working on a project using typescript. This is out first project with typescript. In this project we need to connect to a WFS server and this returns GML to us. Now to parse GML, we think of using the OpenLayers javascript library. I know there is a DefinitelyTyped version of Openlayers, but the GML parsing isn't ready yet.
So we tried to the javascript files. But i think we are missing something, because we are getting an error: Openlayers is undifined.
This is our code:
module PP.Data {
declare var OpenLayers: any;
export class WebRequest {
public GetGML(url: string): XMLDocument {
var retour: XMLDocument;
var _this = this;
$.ajax({
url: url,
type: 'GET',
crossDomain: true,
cache: false,
async: false,
dataType: 'xml'
}).done(function (data) {
var format = new OpenLayers.Format.GML();
retour = format.read(data);
});
return retour;
}
}
}
So the error starts when we try to create an instance of GML.
Any ideas?
Thanks!
OpenLayers is undefined.
Make sure OpenLayers is included (eg using a script tag) before you try to use it from typescript
You are effectively returning undefined
here (as retour isn't assigned before its returned).
.done(function (data) {
var format = new OpenLayers.Format.GML();
retour = format.read(data);
});
return retour;
You should return the promise ie. the return value of the .done
function OR have a callback. It needs to be async all the way down.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.