[GIS] How to label a dynamic KML file in OpenLayers

kmllabelingopenlayers-2

How can I display labels for features in a KML file on an OpenLayers map. I was thinking of using popup messages, but labels are more attractive and eye-catching.

I want to replicate the labeling method of Google Earth:

enter image description here

Any hints? Thanks in advance.

Best Answer

Have you checked the OpenLayers Labeled features example?

enter image description here

        var vectorLayer = new OpenLayers.Layer.Vector("Simple Geometry", {
            styleMap: new OpenLayers.StyleMap({'default':{
                strokeColor: "#00FF00",
                strokeOpacity: 1,
                strokeWidth: 3,
                fillColor: "#FF5500",
                fillOpacity: 0.5,
                pointRadius: 6,
                pointerEvents: "visiblePainted",
                // label with \n linebreaks
                label : "name: ${name}\n\nage: ${age}",

                fontColor: "${favColor}",
                fontSize: "12px",
                fontFamily: "Courier New, monospace",
                fontWeight: "bold",
                labelAlign: "${align}",
                labelXOffset: "${xOffset}",
                labelYOffset: "${yOffset}"
            }}),
            renderers: renderer
        });