Skip to main content

Icon

The Icon object sets up the icon for a marker.

Icon extends Base.

Usage example

const icon = G.icon({
url: 'https://mywebsite.com/images/marker.png',
size: [20, 32]
});
const marker = G.marker({
latitude: 40.730610,
longitude: -73.935242,
icon
});

Creating the Icon object

G.icon(url?: string | IconOptions, options?: IconOptions): Icon

There are a few ways to setup the Icon object.

No parameters.

G.icon(): Icon

const icon = G.icon();

Pass the url and options.

G.icon(url: string, options: IconOPtions): Icon

ParameterTypeRequiredDescription
urlstringYesThe URL for the icon image.
optionsIconOptionsThe icon options.
const icon = G.icon('https://mywebsite.com/images/marker.svg', {size: [20, 32]});

Pass only the options.

G.icon(options: IconOptions): Icon

ParameterTypeRequiredDescription
optionsIconOptionsThe icon options.
const icon = G.icon({
url: 'https://mywebsite.com/images/marker.png',
size: [20, 32]
});

Pass an existing Icon object.

G.icon(object: Icon): Icon

In this case the Icon object is simply returned.

ParameterTypeRequiredDescription
objectIconYesAn Icon object.
const icon = G.icon(iconObject);

IconValue type

When other objects accept an Icon value the value type that they accept is an IconValue.

The IconValue can be one of the following values:

  • An Icon object.
  • A string (The URL for the icon graphic).
  • An IconOptions object.

Icon options

Type IconOptions.

IconOptions is an object containing the configuration options for the Icon object.

OptionTypeDefaultDescription
anchorPointValueThe position at which to anchor an image in correspondence to the location of the marker on the map. By default, the anchor is located along the center point of the bottom of the image.
labelOriginPointValueThe origin of the label relative to the top-left corner of the icon image, if a label is supplied by the marker. By default, the origin is located in the center point of the image.
originPointValueThe position of the image within a sprite, if any. By default, the origin is located at the top left corner of the image (0, 0).
scaledSizeSizeValueThe display size of the sprite or image. When using sprites, you must specify the sprite size. If the size is not provided, it will be set when the image loads.
urlstring''The URL to the icon image (or sprite sheet) itself. All browsers support GIF, JPEG, SVG, and PNG formats. If an SVG is used, the height and width attributtes in the SVG HTML are required. If they are not set then the SVG will not display correctly.

Methods

  • Methods inherited from Base.

setAnchor

setAnchor(anchor: PointValue): Icon

Set the position at which to anchor an image in correspondence to the location of the marker on the map.

ParameterTypeRequiredDescription
anchorPointValueYesThe anchor point value.
const icon = G.icon({
url: 'https://mywebsite.com/images/marker.png',
});
icon.setAnchor([10, 32]);

Valid value types include:

icon.setAnchor([10, 32]);
icon.setAnchor({x: 10, y: 32});
icon.setAnchor(pointObject);

setLabelOrigin

setLabelOrigin(origin: PointValue): Icon

Set the origin of the label relative to the top-left corner of the icon image, if a label is supplied by the marker.

ParameterTypeRequiredDescription
anchorPointValueYesThe label origin value.
icon.setLabelOrigin([10, 32]);
icon.setLabelOrigin({x: 10, y: 32});
icon.setLabelOrigin(pointObject);

setOptions

setOptions(options: IconOptions): Icon

This is an alternate way to set the icon options.

See the icon options section for the available options.

ParameterTypeRequiredDescription
optionsIconOptionsYesThe options to set.
icon.setOptions(\{anchor: [0,-10]\});

setOrigin

setOrigin(origin: PointValue): Icon

Set the position of the image within a sprite, if any. By default, the origin is located at the top left corner of the image (0, 0).

ParameterTypeRequiredDescription
originPointValueYesThe origin point value.
icon.setOrigin([10, 32]);
icon.setOrigin({x: 10, y: 32});
icon.setOrigin(pointObject);

setScaledSize

setScaledSize(size: SizeValue): Icon

Set the scaled size of the icon. Use this if for some reason you didn't pass the scaled size in the icon options.

ParameterTypeRequiredDescription
sizeSizeValueYesThe size value.
icon.setScaledSize([10, 32]);
icon.setScaledSize({x: 10, y: 32});
icon.setScaledSize(sizeObject);

setSize

setSize(size: SizeValue): Icon

Set the size of the icon. Use this if for some reason you didn't pass the size in the icon options.

ParameterTypeRequiredDescription
sizeSizeValueYesThe size value.
icon.setSize([10, 32]);
icon.setSize({x: 10, y: 32});
icon.setSize(sizeObject);

setUrl

setUrl(url: string): Icon

ParameterTypeRequiredDescription
urlstringYesThe icon URL.

Set the icon URL.

icon.setUrl('https://mywebsite.com/images/marker.png');

toGoogle()

toGoogle(): google.maps.Icon

Returns the icon options as the google.maps.Icon interface.

const googleIcon = icon.toGoogle();