Search code examples
openlayersmarker

Openlayers Custom Marker


some info: I got a main layer (map) which and im drawing lines between points recived from a JSON result with a Linestring.

(issue) I followed an example online on how to customize the points i am adding. But this does not work. (Look at the function on the bottom.)

Code:

//'listOfPoints' is an array containing all the point objects.

var pointmap = new OpenLayers.Geometry.LineString(listOfPoints);

    var lastpoint = listOfPoints[listOfPoints.length -1];


    var vesselLayer = new OpenLayers.Layer.Vector(data.bridge_name);

    if (lastpoint != null) {
        var markerLayer = getPOI(lastpoint);
        vesselLayer.addFeatures([pointmap,markerLayer]);
    } else {
        vesselLayer.addFeatures([new OpenLayers.Feature.Vector(pointmap)]);
    }

// Function for creating a marker and returning it to the caller. 

function getPOI(point) {

//This was also tried without the "Style" property. (Only the externalGraphic line)
var temp_feature = new OpenLayers.Feature.Vector(
        point,
        style: { externalGraphic: '/assets/img/marker.png', graphicHeight: 16,     graphicWidth: 16, graphicXOffset:8, graphicYOffset:8  }
    );    

return temp_feature;
}

Solution

  • At first glance, there's error in temp_feature definition:

    var temp_feature = new OpenLayers.Feature.Vector(
        point,
        null,
        {
            externalGraphic: '/assets/img/marker.png',
            graphicHeight: 16,
            graphicWidth: 16,
            graphicXOffset:8,
            graphicYOffset:8
        }
    ); 
    

    It's useful to read and follow API documentation: http://dev.openlayers.org/docs/files/OpenLayers/Feature/Vector-js.html#OpenLayers.Feature.Vector.OpenLayers.Feature.Vector

    OpenLayers.Feature.Vector takes three arguments, geometry, object with attributes (which is null in your case, since you don't have any attributes), and object with style.

    I haven't tested that code, there may be other problems, too.

    And about general use of JavaScript, there is never such thing as

    style: { externalGraphic: '/assets/img/marker.png', graphicHeight: 16,     graphicWidth: 16, graphicXOffset:8, graphicYOffset:8  }
    

    Object is always defined within brackets:

    {style: { externalGraphic: '/assets/img/marker.png', graphicHeight: 16,     graphicWidth: 16, graphicXOffset:8, graphicYOffset:8  }}