[OpenLayers-Users] Questions on using Protocol.HTTP and custom XML to load/edit features

Stephen Woodbridge woodbri at swoodbridge.com
Wed Mar 10 12:21:15 EST 2010


Eric Lemoine wrote:
> On Monday, March 8, 2010, Stephen Woodbridge <woodbri at swoodbridge.com> wrote:
>> OK, to partially answer some of my own questions ... inline below.
>>
>> Stephen Woodbridge wrote:
>>> Hi all,
>>>
>>> I am trying to implement a vector layer that will allow editing point
>>> data. The data is accessed via a PHP script that serves some arbitrary
>>> XML. The layer looks like this so far:
>>>
>>>              vector = new OpenLayers.Layer.Vector("POI", {
>>>                  maxResolution: 360.0/16777216/2, // zoom 15
>>>                  styleMap: styleMap,
>>>                  protocol: new OpenLayers.Protocol.HTTP({
>>>                      url: "poi-server.php",
>>>                      params: {
>>>                          m: 'r',
>>>                          srs: 'EPSG:4326'
>>>                      },
>>>                      format: new OpenLayers.Format.XML()
>>>                  }),
>>>                  strategies: [
>>>                      new OpenLayers.Strategy.BBOX({ratio: 1}),
>>>                      new OpenLayers.Strategy.Save({
>>>                          auto: true//,
>>>                          //save: function() { alert("Save called."); }
>>>                      }),
>>>                  ]
>>>              });
>>>
>>> This seems like it almost works. But has the following problems, so
>>> maybe I'm just taking the wrong approach all together. Here is a live
>>> example:
>>>
>>> http://imaptools.com:8080/tilecache/test.html?zoom=17&lat=33.89595&lon=35.49935&layers=BT
>>>
>>> Issue:
>>>
>>> 1) the feature request does not seem to load on page load, but fires if
>>> you pan the map slightly
>>>
>>> 2) when the features load, I get an error on line 128 of
>>> OpenLayers/Format/XML.js because test.indexOf() is not a method. This is
>>> because text is type Document not string?
>> This appears to be because I need to write my own class like
>> OpenLayers.Format.MyXML that parses the XML document the PHP returns and
>>   returns an array of features. Working on writing this.
>>
>>> 3) I assume the the XML parser will parse the response into an
>>> appropriate tree object, then I need to add some code that reads the
>>> tree and constructs feature objects that I then add to the layer. So is
>>> the problem above because the text object passed to read IS my tree and
>>> I need to pass a my own read() function to some code to do that? Where
>>> do I do that?
>> See 2) above.
>>
>>> 4) If I add a point using the toolbar add point button, I get
>>>
>>> Security error"  code: "1000
>>> http://imaptools.com:8080/ol28/lib/OpenLayers/Format/XML.js
>>> Line 198
>>>
>>> data = serializer.serializeToString(node);
>>>
>>> What is this error and why am I getting it? I assumed that a default
>>> serializer exists, I probably need to add my own it write the expected
>>> XML the the PHP needs. Where would I add this and what are the exected
>>> inputs and outputs?
>> I'm hoping this is probably related to the above. will report back after
>> I get my XML parser working.
>>
>>> 5) I eventually need to issue the Ajax requests by sending XML documents
>>> to the server instead of issuing GET requests. So to do this I assume I
>>> need to a) write code to serialize some params into a document, then b)
>>> get HTTP protocol to send the document. How do I do this? The PHP code
>>> is only temporary for testing and developing in my environment.
>>>
>>> I learned a lot today, just by reading lots of OL code but I still have
>>> a ways to go.
>>>
>>> I would appreciate and help and guidance.
>> Any thoughts on 1) or 5)? Just a pointer would be helpful :)
> 
> Hi Steve.
> 
> For 1) what version of OL are you using? I thought this issue was
> fixed a while back. A test case demonstrating the problem would be
> useful.

I'm running OL 2.8, I have not tried trunk.

http://imaptools.com:8080/tilecache/test.html?zoom=17&lat=33.89595&lon=35.49935&layers=BT

This is a work in progress, that is getting edits, but demonstrates the 
issue.

1) Turn on Firebug
2) load the page
3) verify in the FB->"Net" tab that 
http://imaptools.com:8080/tilecache/poi-server.php?...
has not been called.
4) Either pan view slightly ot toggle Vector layer off/on in layerswitcher
5) Notice in FB->"Net" tab the poi-server.php is called.

> For 5) the HTTP protocol has a readWithPost option that allows sending
> read requests with POST instead of GET. But I don't think it'll work
> in your case because you it will post encoded params, and you won't be
> able to provide it with your XML document. So I think you need to
> implement your own protocol, possibly by extending/overriding
> Protocol.HTTP.

OK, I think I have this part figured out. I am solving this by writing 
OpenLayers.Format.MyXML class. You can see it in the top of my html 
page. So it looks like HTTP protocol implements a REST protocol, so as 
long as my PHP implements a REST interface using the Format, I think it 
will work. I'm still wading neck deep in this code. Format has 
properties readers: {} and writers: {} that deal with namespaces, but 
I'm not sure that I can (or how to) plug into them which lead me to 
writing my own class.

Thank you for your response, I am really trying to figure this out and 
seem to be making some progress. If there is a better way to do any of 
this, please let me know, otherwise, I'm plugging forward. OL is very 
neat. While it is not intuitive to me because I only have to dive into 
it every 4-6 months, it does make sense when I finally get headed in the 
right direction.

Thanks to you, Chris and all the other devs that have put so much work 
into making this a wonderful product.

-Steve



More information about the Users mailing list