[OpenLayers-Users] GPX with empty labels

Martin Mares mj at ucw.cz
Sun Jun 9 14:06:59 PDT 2013


Hello, world!

I am using OpenLayers to display GPX waypoints and tracks on the top
of an OSM layer. The waypoints are named and I want their names to be
rendered as labels, so I set 'label: "${name}"' in the style. However,
the tracks have no names and when I leave them empty in the GPX file,
I get a label "undefined" on the map.

Previously, I worked around this problem by setting the track names
to a single space, but this no longer works with recent OpenLayers:
"NaN" is rendered instead of the space.

After a brief inspection of the code, I think that the problem lies in the
following function in OpenLayers/Style.js:

| OpenLayers.Style.createLiteral = function(value, context, feature, property) {
|     if (typeof value == "string" && value.indexOf("${") != -1) {
|         value = OpenLayers.String.format(value, context, [feature, property]);
|         value = (isNaN(value) || !value) ? value : parseFloat(value);
|     }
|     return value;
| };

On the third line, value is set to " ". Then, isNaN(" ") is false, !" " is
false, but parseFloat(" ") returns a NaN. The problem occurs in Firefox 10 and
Chrome 27, but as far as I understand, this behavior is standards-compliant.

Similarly, when the label is "00", it gets silently normalized to "0", which
is unlikely to be useful.

Am I missing something? Is there any better method to render labels based on
feature attributes?

My code (well, several examples from openlayers.org held together by duct tape)
is attached below for reference.

				Have a nice fortnight
-- 
Martin `MJ' Mares                          <mj at ucw.cz>   http://mj.ucw.cz/
Faculty of Math and Physics, Charles University, Prague, Czech Rep., Earth
int random(void) { return 4; /* Random number chosen by a fair dice roll. */ }

Code:

        map = new OpenLayers.Map("map", {
                projection: new OpenLayers.Projection("EPSG:900913"),
                displayProjection: new OpenLayers.Projection("EPSG:4326")
        });

        map.addLayer(new OpenLayers.Layer.OSM());

        trasa = new OpenLayers.Layer.Vector("Trasa", {
                protocol: new OpenLayers.Protocol.HTTP({
                        url: gpxName,
                        format: new OpenLayers.Format.GPX()
                }),
                projection: new OpenLayers.Projection("EPSG:4326"),
                strategies: [new OpenLayers.Strategy.Fixed()],
                styleMap: new OpenLayers.StyleMap({
                        'default': new OpenLayers.Style({
                                fillColor: "#5555aa",
                                fillOpacity: 1,
                                strokeColor: "#0000ff",
                                strokeWidth: 2,
                                strokeOpacity: 1,
                                pointRadius: 5,
                                label: "${name}",
                                labelYOffset: 15,
                                fontSize: "12px",
                                fontWeight: "bold",
                        }),
                        'temporary': new OpenLayers.Style({
                                fillColor: "#5555aa",
                                fillOpacity: 1,
                                strokeColor: "#ff00ff",
                                strokeWidth: 3,
                                strokeOpacity: 1,
                                pointRadius: 6,
                        }),
                }),
        });
        map.addLayer(trasa);


More information about the Users mailing list