3

I'm using a transparent PNG as my marker, and would like for the transparent area to be filled a certain color. I previously accomplished this using marker shadows, but those don't work with the visual refresh (i.e. v3.14).

Thanks!

mustafa.0x
  • 1,476
  • 2
  • 17
  • 30
  • Make your own custom [Overlay](https://developers.google.com/maps/documentation/javascript/reference#OverlayView). – geocodezip Oct 10 '13 at 12:57
  • @geocodezip I've looked at the docs and the USGS example, but remain unsure how to best implement this approach. Any help would be appreciated. – mustafa.0x Oct 21 '13 at 13:31
  • @geocodezip I've got a 50 point bounty on this question, if you have time for further clarification. – mustafa.0x Dec 25 '13 at 16:30

5 Answers5

3

A trick could be to manipulate the PNG image with PHP, if this is an option. The following script takes 4 parameters: the image source, the amount of red, green and blue.

image.php script:

$src = $_GET['src'];

$r = $_GET['r'];
$g = $_GET['g'];
$b = $_GET['b'];

$image = @imagecreatefrompng($src);

// Create a new true color image with the same size
$w = imagesx($image);
$h = imagesy($image);
$color = imagecreatetruecolor($w, $h);

// Fill the new image with desired color
$bg = imagecolorallocate($color, $r, $g, $b);
imagefill($color, 0, 0, $bg);

// Copy original transparent image onto the new image
imagecopy($color, $image, 0, 0, 0, 0, $w, $h);

// Serve the image
header("Content-type: image/png");
imagepng($color);
imagedestroy($color);

In javascript, call image.php with the desired parameters:

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(0, 0),
    map: map,
    icon: 'path/to/image.php?src=http://maps.google.com/mapfiles/marker.png&r=100&g=125&b=255'
});

Original image:

Original image

Output image:

Output image

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
  • I'm hoping for something Javascript only, but thanks, this is a possibility I guess. – mustafa.0x Dec 26 '13 at 06:58
  • 2
    Have a look at this example: http://blog.mridey.com/2009/09/label-overlay-example-for-google-maps.html it is about creating a marker label (OverlayView) under the marker. Maybe you can adapt this to your needs. – MrUpsidown Dec 26 '13 at 11:04
  • 1
    Thanks, your link provided sufficient inspiration. :) – mustafa.0x Dec 30 '13 at 07:37
1

Set the optimized-option of the marker to false, then you may apply custom css to the marker-img by using the selector:

img[src="path/to/custom/marker.png"]

<edit/>

to be able to use the same image with multiple colors simply add the color as URI-fragment, then you will get a unique selector even with the same image:

JS:

new google.maps.Marker({
  optimized: false,
  icon: 'path/to/custom/marker.png#red'
  //other properties
});

CSS:

#map-canvas img[src="path/to/custom/marker.png#red"]{
 background-color:red;
}

Of course the CSS-rules may also be set dynamically via JS when you don't want to hardcode them, see: styleSheet.insertRule()

Demo with multiple background-colors for the same image(including dynamic rule-insertion) :

http://jsfiddle.net/doktormolle/vngp1hdr/

Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
  • This solution isn't very suitable as multiple markers use the same marker icon. – mustafa.0x Oct 21 '13 at 13:28
  • Not sure if a version update changed this, but I can't seem to find any img tags with the icon; possibly they are served as background images, so this won't work any more? – kontur Feb 04 '15 at 11:38
  • It still works for me, added a demo(note that the `optimized`-option of the markers must be set to `false`). – Dr.Molle Feb 04 '15 at 15:59
1

I think Dr. Molle is right

You simply just apply css to the marker image.

To make the marker not to use canvas, you need to set optimized to false.

var marker = new google.maps.Marker({
  position: new google.maps.LatLng(-34.397, 150.644),
  map: map,
  optimized: false,
  icon: "https://i.stack.imgur.com/su8w5.png"
});

With just css set, you can apply background to all markers.

img[src="path/to/custom/marker.png"] {
   background-color: black;
}

I am using Tomas example, and modified it

http://jsfiddle.net/Qrj2n/2/

allenhwkim
  • 27,270
  • 18
  • 89
  • 122
0

I have encountered the quite similar issue. I wish to convert my icon background to to transparent instead.

I believe the easiest way is to convert your existing icon (png,jpg,bitmap) to gif format by making the background transparent. You can use the following online tool to help you do it. It is extremely easy.

The following is the example of the icon with transparent output in google map. Hope it helps.

ct.tan
  • 2,355
  • 1
  • 11
  • 4
-3

OK, where's the difficulty? If you want the semi-transparent colors, just create then in the icon, like here I have 50% transparent green:

enter image description here

And then put it on the map:

http://jsfiddle.net/Qrj2n/

var marker = new google.maps.Marker({
      position: new google.maps.LatLng(-34.397, 150.644),
      map: map,
      icon: "https://i.stack.imgur.com/su8w5.png"
});
Tomas
  • 57,621
  • 49
  • 238
  • 373
  • 1
    I want to set the background color *of the marker*, not of the icon. That way, I don't have to create an icon for every hex color that exists. – mustafa.0x Dec 26 '13 at 06:55
  • @mustafa.0x then you should be more clear in your question. You wrote *"I'm using a transparent PNG as my marker, and would like for the transparent area to be filled a certain color" - which suggest you are talking about transparent area of the PNG! Sorry but then its a poorly written question. You should describe what you want and why, we are not here to guess your mind. – Tomas Dec 27 '13 at 17:42
  • Haha, cheeky, aren't you? :) I didn't downvote you; no need to be snarky. – mustafa.0x Dec 27 '13 at 18:34
  • @mustafa.0x I know it wasn't you :) It's not a revenge. It's that I just really think that your question is poorly written. BTW my answer would be a solution to the question as you wrote it. – Tomas Dec 27 '13 at 19:18