Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change marker size when using Google Maps setIcon

I am adding markers to a map, which are small when I originally add them. However, I then loop over them and change some of them to another marker that has the same size but a different color:

HTML

<div class="row">
  <div class="small-6 columns">
    <select id="state-select" class="">
          <option value="">SELECT A STATE</option>
          <option value="OH">Ohio</option>
          <option value="IL">Illinois</option>
          <option value="IN">Indiana</option>
          <option value="MO">Missouri</option>
          <option value="MI">Michigan</option>
    </select>
  </div>
</div>
<div id="map-canvas"></div>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places" type="text/javascript"></script>

JS:

$.each(stateMarkerArr, function(index, thisStateMarker) {
    if (thisStateMarker.state == stateCode) {
        thisStateMarker.setIcon('/img/example_img.png');        
        bounds.extend(thisStateMarker.getPosition());           
    } else {
        thisStateMarker.setIcon('/img/example_img2.png');       
    }
});

However, the marker is being plotted as in a larger size:

enter image description here

How can I dynamically set the size of the marker when using the setIcon function, so that I can make these two markers the same size?

I was setting the original points with:

var icon = {
    url: '/img/example_img.png',
    scaledSize: new google.maps.Size(28,50)
}
var marker = new google.maps.Marker({
    position: currLatlng,
    map: map,
    title: thisLocation.Store,
    state: currentState,
    icon: icon
});

AND then trying to set the points on the fly with:

$.each(stateMarkerArr, function(index, thisStateMarker) {
    if (thisStateMarker.state == stateCode) {
        var icon = {
            url: '/img/example_img.png',
            scaledSize: new google.maps.Size(28,50)
        }       
        thisStateMarker.setIcon(icon);
        bounds.extend(thisStateMarker.getPosition());           
    } else {
        var icon = {
            url: '/img/example_img2.png',
            scaledSize: new google.maps.Size(28,50)
        }       
        thisStateMarker.setIcon(icon);              
    }
});
like image 678
maudulus Avatar asked Apr 01 '16 14:04

maudulus


People also ask

How do I change the marker size on Google Maps?

You can't modify the default marker size. You need to make a custom marker that uses the same icon and change the size of that. You can check the documentation here on how to use custom marker icons: developers.google.com/maps/documentation/javascript/…


1 Answers

You can use the size or scaledSize properties of the google.maps.Icon object. See the Docs and this example:

var icon = {url:'/img/example_img.png', size: new google.maps.Size(20, 32)};
thisStateMarker.setIcon(icon);
like image 185
malix Avatar answered Sep 19 '22 13:09

malix