Function to load style
static final StyleFactory STYLE_FACTORY = CommonFactoryFinder.getStyleFactory();
protected Style loadStyleDynamic(String baseUrl) throws MalformedURLException, UnsupportedEncodingException {
UriBuilder uriBuilder = UriBuilder.fromUri(baseUrl);
URL url = new URL(uriBuilder.toString());
PointSymbolizer symb = STYLE_FACTORY.createPointSymbolizer();
ExternalGraphic eg = STYLE_FACTORY.createExternalGraphic(url, "image/svg+xml");
symb.getGraphic().graphicalSymbols().add(eg);
Literal size = FILTER_FACTORY.literal(scaleFactor);
symb.getGraphic().setSize(size);
Rule rule = STYLE_FACTORY.createRule();
rule.symbolizers().add(symb);
FeatureTypeStyle fts = STYLE_FACTORY.createFeatureTypeStyle(rule);
Style style = STYLE_FACTORY.createStyle();
style.featureTypeStyles().add(fts);
return style;
}
Then I am adding this style to the feature collection object of the point at which I want to load the marker.
SVG Marker
<svg xmlns='http://www.w3.org/2000/svg' width='122' height='28' viewBox='0 0 122 28' fill='none'><rect x='1' y='1' width='120' height='26' rx='13' fill='#27A6A4'/><rect x='1' y='1' width='120' height='26' rx='13' stroke='#022D42' stroke-width='1.5'/><foreignObject width='122' height='28'><body xmlns='http://www.w3.org/1999/xhtml' style='margin:0;padding:0'><p style='margin: 0;display: block;text-overflow: ellipsis;overflow: hidden;white-space: pre;padding: 7px;text-align: center;color: rgb(2, 45, 66);font-family: Arial, Helvetica, sans-serif;font-size: 12px;font-weight: 600;'>Manhattan Midtown, NY</p></body></foreignObject></svg><svg xmlns='http://www.w3.org/2000/svg' width='122' height='28' viewBox='0 0 122 28' fill='none'><rect x='1' y='1' width='120' height='26' rx='13' fill='#27A6A4'/><rect x='1' y='1' width='120' height='26' rx='13' stroke='#022D42' stroke-width='1.5'/><foreignObject width='122' height='28'><body xmlns='http://www.w3.org/1999/xhtml' style='margin:0;padding:0'><p style='margin: 0;display: block;text-overflow: ellipsis;overflow: hidden;white-space: pre;padding: 7px;text-align: center;color: rgb(2, 45, 66);font-family: Arial, Helvetica, sans-serif;font-size: 12px;font-weight: 600;'>Som Long Text For Ellipses</p></body></foreignObject></svg>
SVG rendered on final image:
The text inside the SVG is not rendered. I think there is some issue with geotools to render SVGs with foreign objects.