14

I'm playing with the Google Maps API (v3) and I've run into an issue with marker icons. I'm trying to vary the size of my markers depending on their individual data attributes.

The icons themselves are in a sprite that contains three different circular markers, each 16px by 16px. I'm trying to scale individual icons, but am so far unsuccessful.

Here's my code:

var offset = Math.floor(Math.random() * 3) * 16; // pick one of the three icons in the sprite

// Calculate desired pixel-size of the marker
var size = Math.floor(4*(count-1) + 8);

// Create custom marker
var marker = new google.maps.Marker({
    position: new google.maps.LatLng(lat, lng),
    icon: new google.maps.MarkerImage(
        'img/dot.png', // my sprite with 3 circular icons
        new google.maps.Size(16, 16), // 16x16 is the original size of each individual icon
        new google.maps.Point(0, offset), // picking one of the three icons in the sprite
        new google.maps.Point(Math.floor(size/2), Math.floor(size/2)), // setting correct anchor for the marker
        new google.maps.Size(size, size) // trying to scale the marker
       )
    });

The problem seems to be at the last line, where I'm trying to scale the marker icon to the desired size. Instead of it scaling properly, I'm getting a weird, distorted ellipse-shaped icon.

What am I doing wrong?

Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
  • this has been deprecated. See "Converting MarkerImage objects to type Icon" on https://developers.google.com/maps/documentation/javascript/overlays – Ray Apr 15 '13 at 17:56

1 Answers1

22

After some trial and error, I have figured out how this is supposed to work (the documentation is deceiving), thanks to this blog post.

Here's my new code:

var offset = Math.floor(Math.random() * 3) * 16; // pick one of the three icons in the sprite

// Calculate desired pixel-size of the marker
var size = Math.floor(4*(count-1) + 8);
var scaleFactor = size/16;

// Create custom marker
var marker = new google.maps.Marker({
    position: new google.maps.LatLng(lat, lng),
    icon: new google.maps.MarkerImage(
        'img/dot.png', // my 16x48 sprite with 3 circular icons
        new google.maps.Size(16*scaleFactor, 16*scaleFactor), // desired size
        new google.maps.Point(0, offset*scaleFactor), // offset within the scaled sprite
        new google.maps.Point(size/2, size/2), // anchor point is half of the desired size
        new google.maps.Size(16*scaleFactor, 48*scaleFactor) // scaled size of the entire sprite
       )
    });

Hope this helps someone down the line!

Community
  • 1
  • 1
Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
  • 5
    For those following along at home, the salient point from the linked blog article is: "To display a scaled image or a scaled prite image, just supply the size, origin, anchor and scaledSize parameters with the scale factor applied." – natevw May 08 '12 at 23:34
  • thanks, fyi google.maps.MarkerImage is replaced by google.maps.Icon in v3.10+ – helmi03 Apr 16 '14 at 03:55
  • 1
    I use scaledSize to downsize the icon used for marker. That works for me. var icon = { url: imageName, scaledSize: new google.maps.Size(8, 12) }; – user908645 Apr 16 '15 at 06:04