[OpenLayers-Users] OpenLayers Ajax question

Andreas Hocevar ahocevar at opengeo.org
Wed Jun 17 04:19:32 EDT 2009


Hi,

On Wed, Jun 17, 2009 at 2:01 AM, Nicholas
Efremov-Kendall<n.e.kendall at gmail.com> wrote:
> I've got a pretty simple Ajax function working on my page. Essentially, I
> have a text field which fires ajax calls onkeyup, and when the results are
> found, they get dumped into a div below. What I'd like to do, is have the
> results of those searches simultaneously get dumped into the map. I've got
> my head wrapped around how to get the php script to format the  querry into
> Gml or alternatively WKT, but I'm not sure how to feed the data to the map
> div and how to deal with the next set of points getting loaded. I was
> thinking of chaining a function connected to the ajax request, something
> like an add features script.
>
> Has anyone done something like this or can someone suggest how to deal with
> the response text?

Of course there are many ways to do this, but the one with the least
coding efforts is probably to use vector behaviors. First of all, have
a look at the source code of [1]. This is our starting point. The
thing you will want to behave differently is that new features should
be added to the layer instead of replacing the existing features.
Normally, to load your data, you would just say

layer.strategies[0].load({
   url: "your/url/here",
   params: {/* request parameters here */}
});

But since you do not want the features replaced, you would either have
to override the strategy's merge method, or just do a protocol.read
with a custom callback to add new features:

layer.protocol.read({
   url: "your/url/here",
   params: {/* request parameters here */}
   callback: function(resp) {
       var features = resp.features;
       if(features && features.length) {
           layer.addFeatures(features);
       }
   }
});

If your features are in a different projection than the map, you need
to set the format options "internalProjection" to the map's projection
and "externalProjection" to the projection of your data (both in
format: new OpenLayers.Format.GML(...) in the example).

Also, if you decide to use WKT instead of GML, you have to say format:
new OpenLayers.Format.WKT(...) instead.

Note that the above is untested, but it should get you there.

Regards,
Andreas.

[1] http://www.openlayers.org/dev/examples/behavior-fixed-http-gml.html

--
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.



-- 
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.



More information about the Users mailing list