Ian Beeson Ian Beeson - 3 months ago 35
Javascript Question

Here Maps JavaScript API 3.0 Explorer How to set marker color

I can't find any documentation or examples for setting a markers colour in version 3.0 API. you could do this with the 2.5, do any one know if marker colour is supported anymore

function addMarkersToMap(map) {
var parisMarker = new H.map.Marker({ lat: 55.560789, lng: 12.981141 });
map.addObject(parisMarker);
}

Answer

As stated in the example in the API Explorer, creating an H.map.Marker without specifying an icon results in a default image. If you need to have different colored icons, you will need to create them using SVG Graphics.

function addSVGMarkers(map){
  var svgMarkup = '<svg style="left:-14px;top:-36px;"' +
      'xmlns="http://www.w3.org/2000/svg" width="28px" height="36px" >' +
      '<path d="M 19 31 C 19 32.7 16.3 34 13 34 C 9.7 34 7 32.7 7 31 C 7 29.3 9.7 ' +
      '28 13 28 C 16.3 28 19 29.3 19 31 Z" fill="#000" fill-opacity=".2"></path>' +
      '<path d="M 13 0 C 9.5 0 6.3 1.3 3.8 3.8 C 1.4 7.8 0 9.4 0 12.8 C 0 16.3 1.4 ' +
      '19.5 3.8 21.9 L 13 31 L 22.2 21.9 C 24.6 19.5 25.9 16.3 25.9 12.8 C 25.9 9.4 24.6 ' +
      '6.1 22.1 3.8 C 19.7 1.3 16.5 0 13 0 Z" fill="#fff"></path>' +
      '<path d="M 13 2.2 C 6 2.2 2.3 7.2 2.1 12.8 C 2.1 16.1 3.1 18.4 5.2 20.5 L ' +
      '13 28.2 L 20.8 20.5 C 22.9 18.4 23.8 16.2 23.8 12.8 C 23.6 7.07 20 2.2 ' +
      '13 2.2 Z" fill="${COLOR}"></path>' +
      '<text transform="matrix( 1 0 0 1 13 18 )" x="0" y="0" fill-opacity="1" ' +
      'fill="#fff" text-anchor="middle" ' +
      'font-weight="bold" font-size="13px" font-family="arial">${TEXT}</text></svg>'

  // Add the first marker
  var parisIcon = new H.map.Icon(
    svgMarkup.replace('${COLOR}', 'blue').replace('${TEXT}', 'P')),
    parisMarker = new H.map.Marker({lat: 55.5607, lng: 12.9811 },
      {icon: parisIcon});

  map.addObject(parisMarker);
}

To update the color, just create a new H.map.Icon replace the icon attribute of the marker.