Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-colored Google Map SVG Symbols

Tags:

maps

svg

A Google Map marker can take a complex svg path as its icon as in something like:

var baseSvg = {
    x1 : "m 0,0 l45,0 l 190,225 l -45,0 l -190,-225 z",
    x2 : "m 225,0 l -45,0 l -190,225 l 45,0 l 190,-225 z"                
};

var baseIcon = {
    path: "M0,0 " + baseSvg["x1"] + baseSvg["x2"],
    fillColor: "#000000",
    fillOpacity: 1,
    scale: .2,
    strokeColor: "black",
    strokeWeight: 0,
    rotation: 15
};

which is then fed into a marker:

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(somelat, somelng),
    icon: baseIcon 
});

All good. But it only draws in a single colour (black in this example). So, can they only have a single colour or is there a way to have multi-coloured symbols? Using the example code, x1 would be red and x2 would be green.

Note that this construct is borrowed from here: How do I indicate transparency in an SVG path in Google Maps API v3? and it works nicely.

like image 756
Andrew-NZ Avatar asked Jan 25 '13 02:01

Andrew-NZ


1 Answers

I just did the same thing and I think you have to draw two markers with essentially identical data, but with the path and in this case fillColor properties changed:

var baseSvg = {
        x1 : "m 0,0 l45,0 l 190,225 l -45,0 l -190,-225 z",
        x2 : "m 225,0 l -45,0 l -190,225 l 45,0 l 190,-225 z"                
    },
    baseIcon = {
        fillOpacity: 1,
        scale: .2,
        strokeColor: "black",
        strokeWeight: 0,
        rotation: 15
    },
    markerPos = new google.maps.LatLng(somelat, somelng),
    // psuedo code for cloning an object since that 
    // is out of scope for this question
    greenIcon = Object.clone(baseIcon),
    redIcon = Object.clone(baseIcon);

greenIcon.path = baseSvg.x1;
greenIcon.fillColor = "#0f0";
redIcon.path = baseSvg.x2;
redIcon.fillColor = "#f00";

var marker1 = new google.maps.Marker({
    position: markerPos,
    map: map,
    icon: greenIcon
});
var marker2 = new google.maps.Marker({
    position: markerPos, 
    map: map,
    icon: redIcon
});

obviously not optimized js, but you get the idea.

like image 68
hellatan Avatar answered Oct 21 '22 09:10

hellatan