[OpenLayers-Users] OpenLayers.Layer.Text - Place marker on click without file

Alex G. unseen.wizard at gmail.com
Thu Oct 8 09:55:24 EDT 2009


The problem with Layer.Text is that it accepts a tab delimited file while I
wanted to pass coords, a title and a description for a marker.

I was able to do do that with adding a few parameters to location and with
some editing of the Text.js file.

Here are the new variables:

    layerToUse:null,
    newLon:null,
    newLat:null,
    newTitle:null,
    newDesc:null,
    newIcon:null,

layerToUse places the marker on an existing layer instead of creating a new
one (functionality I was looking for).

And also changed the following code:

    loadText: function() {
        if (!this.loaded) {
            if (this.location != null) {

                var onFail = function(e) {
                    this.events.triggerEvent("loadend");
                };

                this.events.triggerEvent("loadstart");
                OpenLayers.Request.GET({
                    url: this.location,
                    success: this.parseData,
                    failure: onFail,
                    scope: this
                });
                this.loaded = true;
            }
            else if (this.newTitle != null && this.newDesc != null) {
                this.parseData();
            }
        }
    },

    parseData: function(ajaxRequest) {
        if (ajaxRequest != null) {
            var text = ajaxRequest.responseText;
        } else {
            if(this.newIcon == null) this.newIcon = "";
            var text = "point\ttitle\tdescription\ticon\n" + this.newLat +
"," + this.newLon + "\t" + this.newTitle + "\t" + this.newDesc + "\t" +
this.newIcon + "\n";
        }
        var options = {};

as well as

            data['overflow'] = feature.attributes.overflow || "auto";
            var markerFeature = new OpenLayers.Feature(this.layerToUse,
location, data);
            this.features.push(markerFeature);
            var marker = markerFeature.createMarker();
            if ((feature.attributes.title != null)
                && (feature.attributes.description != null)) {
              marker.events.register('click', markerFeature,
this.markerClick);
            }
            if (this.layerToUse != null) {
                this.layerToUse.addMarker(marker);
            } else {
                this.addMarker(marker);
            }


Also from my HTML file I call:

    var new4 = new OpenLayers.Layer.Text( "Texts", { layerToUse: markers,
newLat: '4420851.767102', newLon: '698762.34033203', newTitle: "Test Title
2", newDesc: "This is a description 2"} );
    new4.parseData();

Instead of:

   map.addLayer(new4);

In summary, what this does is, it feeds specific layer, coords, title and
description to the function.

I also discovered a nice little bug. If you hide an overlay while a marker
bubble is open, the marker disappears while the bubble still stays there.
This also happens on the OL examples (markersTextLayer.html).

I hope I didn't go the long way around to solving this problem...

On Thu, Oct 8, 2009 at 3:29 PM, Christopher Schmidt <crschmidt at metacarta.com
> wrote:

> On Thu, Oct 08, 2009 at 01:27:30PM +0300, Alex G. wrote:
> > Hi all,
> >
> > I am interesting in constructing a function that places a marker with a
> > bubble text as provided by OpenLayers.Layer.Text but I want to provide
> > coordinates, title and description on the fly (without saving them to a
> file
> > and then loading them to the function).
> >
> > I am using the OpenLayers example to catch clicks and then place a simple
> > marker using markers.addMarker(new OpenLayers.Marker(new
> > OpenLayers.LonLat(lonlat.lon,lonlat.lat),icon)); and everything seems to
> > working fine.
> >
> > But I cannot seem to be able to figure out a way to do the same for
> > OpenLayers.Layer.Text.
>
> Er... It shouldn't be any different? Can you share your current code
> that isn't working?
>
> -- CHris
>
> > I am guessing some heavy modification of Layer/Text.js is required? Is
> there
> > maybe a patch around that could help me on this since my Javascript
> > knowledge is pretty bad.
> >
> > Thanks in advance!
>
> > _______________________________________________
> > Users mailing list
> > Users at openlayers.org
> > http://openlayers.org/mailman/listinfo/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20091008/8c96d9ac/attachment.html


More information about the Users mailing list