Just wondering how you specify different markers on a map?

I love the fact that OpenLayers can show multiple content types on the one map (something I couldn't do with GMap), but I'd like to be able to differentiate between the different content types on the map either by having different coloured markers, or different icons/images.

The demo site does this, as well as having different _size_ markers... How is this done?

Comments

phayes’s picture

Add something like this code to your feature style code:

$style = array();
if ($record->node_type == 'cbr_organization') {
  $style['externalGraphic'] = base_path().file_directory_path().'/organization.png';
} else {
  $style['externalGraphic'] = base_path().file_directory_path().'/flag.png';
}
$style['graphicHeight'] = 20;
$style['graphicWidth'] = 20;
$style['graphicOpacity'] = 1;
return $style;

See the docs to see all the properties available for $style.

BWPanda’s picture

Ok, I think I'm starting to understand how this works now...

One more question: where is the documentation for $record? The help text for the 'Feature Styles function' textbox in Views mentions $fields and $row, but not $record...

phayes’s picture

BWPanda: I just updated the 'Feature Styles function' textbox it so that it now says "$record". It was wrong. :-)

btw you can safetly do var_dump($record); and var_dump($fields) - which I find very helpful.

BWPanda’s picture

Status: Active » Fixed

I actually prefer dsm(), but thanks :)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

cpaszkiewicz’s picture

Status: Closed (fixed) » Active

phayes,
where do you add to the 'Feature Style Code'? I can't work it out. I'm using 7.x-2.0-beta1
Thanks so much

marc.groth’s picture

Hey cpaskeiwicz, check out http://drupal.org/node/677946#comment-5959198

1. Basically you need to add the custom field in your 'OpenLayers Data Overlay' view display (in my case it was 'Content: Type').
Then click 'Settings' under 'Format' (just below 'Title') in your 'OpenLayers Data Overlay' display. Scroll down and click on 'Attributes and Styling' fieldset. Here you will see the format of the output of all variables. Make a note of the one you are interested in (in this case $(type_rendered}).

2. Now leave your view and go to the 'OpenLayers' settings page and add a new 'Style' (from the 'Styles' tab).
This is where you are going to use what you just created in the view... Wherever you want it to replace the value dynamically simply insert the above variable name there. An example should make this clearer...

Say you have a content type field (from the first part) and you want each URL to pick up the content type as part of the icon URL, simply enter the URL under 'externalGraphic'. Something like: images/icons/icon-${type_rendered}.png
NOTE the ${type_rendered} bit... That's the variable name and the thing that makes the icon display differently depending on your circumstances. Test it by visiting your map and you should see that each icon now will have the URL you entered above, with the {$type_rendered} bit replaced by the actual content type.

Hope that helps (sorry if it's confused you further!)

Please close this issue if you've got it working.

sambonner’s picture

Is it still possible to implement custom styles markers in code as outlined in #1 using openlayers 7.x-2.x? I can implement the style in code fine, but I don't have access to $record, $field, or $row. I've added an additional field to my openlayers data overlay view display and when I preview the view it prints the additional parameter for use, however it doesn't appear I have access to it in hook__openlayers_styles(). I need to use some complicated logic to conditionally change the marker graphic so using a simple token to switch marker images isn't going to work for me, hence my desire to achieve this programatically.

Thanks,
Sam

sambonner’s picture

OK this answer on a different issue indicates the old objects are no longer available - http://drupal.org/node/1000012#comment-3920478

But apparently using a style plugin (which I am, I thought) one can get access to these field objects - so my question still stands - how? The documentation here https://drupal.org/node/1585880 is great if you are wanting to achieve a custom marker via UI only methods, but makes no mention of how to get these fields within the scope of a style plugin...

sambonner’s picture

For anyone else who might be confused as I was, the documentation here - http://drupal.org/node/620602 does NOT create a style plugin, just a style (which is different, apparently). Style plugins are supported in the 6.x-2.x version of openlayers only. There is no support for this in 7.x-2.x. Apparently this leaves developers running 7.x-2.x with no option but hacky solutions to get custom markers if your requirements are more complex than simple token replacement.

Pol’s picture

I think it's easy to do in 7.x-2.x, I will try to take care of this asap...

sambonner’s picture

Thanks Pol, that would be great, I'd be happy to assist if its useful :)

I've a follow up question to this however. I've written a slightly hacky solution to switch marker graphics conditionally (in this case I need to compare a date field to a current time and switch conditionally).

The actual logic works fine, however only the one marker renders on the map because it appears the graphic location is per-map rather than per marker. I'm doing this switching within hook_openlayers_styles.

Is it possible to have multi different marker graphics on one map? Or is my problem that I am trying to achieve this via hook_openlayers_styles which is wrong?

I'm confused as it is effectively the same method as what is outlined in the documentation here - https://drupal.org/node/1585880 just within code so I can have more complex switching logic.

Thanks,
Sam

sambonner’s picture

Ignore, figured this out, additional overlays + views to the rescue :) !

Pol’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.