Search code examples
javascriptapiiconsmapsmarker

Maps API icon.scaledSize not working


I want to make Maps API icon.scaledSize work like in JSFiddle https://jsfiddle.net/z2mzzry4/1/, but i can't. Icons remain at the same size.

I'm working in the folowing code:

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      #map {
        height: 100%;
      }
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      var map;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -3.5679, lng: -39.12},
          zoom: 8
        });
        var marker2 = new google.maps.Marker
        ({
          position: new google.maps.LatLng(-3.5679, -39.12),
          title: 'Sample 2',
          draggable: false,
          map: map,
          icon: 'http://icons.iconarchive.com/icons/paomedia/small-n-flat/48/map-marker-icon.png',
        });
        marker2.icon.scaledSize = new google.maps.Size(2 * 50, 2 * 50);
        var marker3 = new google.maps.Marker
        ({
          position: new google.maps.LatLng(-3.5679, -39.1295),
          title: 'Sample 3',
          draggable: false,
          map: map,
          icon: 'http://icons.iconarchive.com/icons/paomedia/small-n-flat/48/map-marker-icon.png',
         });
         marker3.icon.scaledSize = new google.maps.Size(3 * 50, 3 * 50);
      }
    </script>
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initMap"></script>
  </body>
</html>

I've browsed in many internet sites, but I can't figure out the problem.

What's wrong?


Solution

  • If you pass an object literal instead of a string to the icon property in the constructor, it should resolve the issue

    var marker2 = new google.maps.Marker({
        position: new google.maps.LatLng(-3.5679, -39.12),
        title: 'Sample 2',
        draggable: false,
        map: map,
        icon: {
            url: 'http://icons.iconarchive.com/icons/paomedia/small-n-flat/48/map-marker-icon.png'
        }
    });
    
    marker2.icon.scaledSize = new google.maps.Size(2 * 50, 2 * 50);