I have a json file describing features on a map (routes, points etc)
points include places of interest such as airport, harbor etc.
Is there a way to assign icons to these points?
example: have a path to an icon in my json file as an attribute to that point and request that that icon be used as a marker for that point?
I used the advice below by @Aragon, still can't get it to work..
my json looks like this:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": "id0",
"properties": {
"graphic": "../../icons/pushpin.png",
"title": "blah",
"transportation": "Blah",
"description": "blah"
},
"geometry": {
"type": "Point",
"coordinates": [
24.067939338686,
35.525197669582
]
},
"crs": {
"type": "name",
"properties": {
"name": "EPSG: 900913"
}
}
}
]}
and my javascript looks like this:
var context = {
getGraphic: function(feature) {
return feature.attributes.graphic;
}
};
var template = {
'externalGraphic': '${getGraphic}',
'graphicWidth' :15,
'graphicHeight':15,
'fillColor':'#669933',
'fillOpacity':.8,
'strokeColor':'red',
'strokeWidth':5,
'pointRadius':6
};
var data_vector_style = new OpenLayers.Style(template, {context: context});
// Style Map tells vector Layer what style objects to use when features are
// in a certain state (states can be default, select or temporary
var data_style_map = new OpenLayers.StyleMap({'default':data_vector_style});
var dataLayer = new OpenLayers.Layer.Vector("My GeoJson data from server", {
projection: new OpenLayers.Projection("EPSG:4326"),
styleMap : data_style_map
});
var features = getFeatures("../../controller?action=GET_USER_LOCATIONS");
//alert(features);
dataLayer.addFeatures(features);
if I change any of the other styling in template, it works fine. if I hardcode the marker in the styling 'externalGraphic': '../../icons/pushpin.png', it works fine. when I try to use attribute replacement the marker isn't used… Am I doing it wrong in the json object?
Best Answer
you can do this with some ways:
defining marker when adding all points from json for icon style:
change it after adding to map:
using marker setUrl method here
i hope it helps you..