From 2c743d0c17a2f4781769ffbeb9531a6c8d4f60c8 Mon Sep 17 00:00:00 2001 From: Marcus Winkler Date: Thu, 6 Feb 2014 00:40:54 +0100 Subject: [PATCH] replaced L.icon containing data-URI-images with L.divIcon --- images/marker-icon.svg.template | 12 +++++++++++ plugins/draw-tools.user.js | 35 +++++++++++---------------------- 2 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 images/marker-icon.svg.template diff --git a/images/marker-icon.svg.template b/images/marker-icon.svg.template new file mode 100644 index 00000000..26b8da11 --- /dev/null +++ b/images/marker-icon.svg.template @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/plugins/draw-tools.user.js b/plugins/draw-tools.user.js index c8cfc05e..d07c4ebe 100644 --- a/plugins/draw-tools.user.js +++ b/plugins/draw-tools.user.js @@ -35,38 +35,27 @@ window.plugin.drawTools.loadExternals = function() { } window.plugin.drawTools.getMarkerIcon = function(color) { - -console.log('getMarkerIcon("' + color + '")'); - if (typeof(color) === "undefined" || color.search(/^[0-9A-Fa-f]{6}$/) == -1) { - // TODO maybe improve error handling here. + if (typeof(color) === 'undefined') { console.warn('Color is not set or not a valid color. Using default color as fallback.'); - color = "a24ac3"; + color = '#a24ac3'; } - // As the length of the color string is 6 charaters which translates to 8 base64 encodes characters - // and we ensured that the color in the svg template is properly aligned we can construct the - // resulting base64 encoded svg by replacing the color inside the base64 encoded template! - var svgIcon = 'data:image/svg+xml;base64,' + - 'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4NCjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIg0KCXZlcnNpb249IjEuMSIgYmFzZVByb2ZpbGU9ImZ1bGwiDQoJd2lkdGg9IjUwcHgiIGhlaWdodD0iODJweCIgdmlld0JveD0iMCAwIDUwIDgyIj4NCg0KCTxwYXRoIGQ9Ik0yLjcyNDgzNjg5NTMsMzcuMzQ5NzYyNDkzNSBBMjUsMjUg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ryb2tlOiNmZmZmZmY7IHN0cm9rZS13aWR0aDoyLjVweDsgc3Ryb2tlLW9wYWNpdHk6IDE7IGZpbGw6IG5vbmU7IiAvPg0KDQoNCjwvc3ZnPg=='; + var svgIcon = window.plugin.drawTools.markerTemplate.replace(/%COLOR%/g, color); - // As we have a svg icon, we can use the same icon for the regular and the retina version. - return L.icon({ - iconUrl: svgIcon, - iconRetinaUrl: svgIcon, + return L.divIcon({ iconSize: new L.Point(25, 41), iconAnchor: new L.Point(12, 41), - popupAnchor: new L.Point(1, -34), - shadowSize: new L.Point(41, 41), - // L.icon does not use the option color, but we store it here to + html: svgIcon, + className: 'leaflet-iitc-custom-icon', + // L.divIcon does not use the option color, but we store it here to // be able to simply retrieve the color for serializing markers - color: '#' + color + color: color }); } window.plugin.drawTools.currentColor = '#a24ac3'; -window.plugin.drawTools.currentMarker = window.plugin.drawTools.getMarkerIcon('a24ac3'); +window.plugin.drawTools.markerTemplate = '@@INCLUDESTRING:images/marker-icon.svg.template@@'; +window.plugin.drawTools.currentMarker = window.plugin.drawTools.getMarkerIcon(window.plugin.drawTools.currentColor); window.plugin.drawTools.setOptions = function() { @@ -99,7 +88,7 @@ window.plugin.drawTools.setOptions = function() { window.plugin.drawTools.setDrawColor = function(color) { window.plugin.drawTools.currentColor = color; - window.plugin.drawTools.currentMarker = window.plugin.drawTools.getMarkerIcon(color.substr(1)); + window.plugin.drawTools.currentMarker = window.plugin.drawTools.getMarkerIcon(color); window.plugin.drawTools.drawControl.setDrawingOptions({ 'polygon': { 'shapeOptions': { color: color } }, @@ -269,7 +258,7 @@ window.plugin.drawTools.import = function(data) { break; case 'marker': var extraMarkerOpt = {}; - if (item.color) extraMarkerOpt.icon = window.plugin.drawTools.getMarkerIcon(item.color.substr(1)); + if (item.color) extraMarkerOpt.icon = window.plugin.drawTools.getMarkerIcon(item.color); layer = L.marker(item.latLng, L.extend({},window.plugin.drawTools.markerOptions,extraMarkerOpt)); break; default: