N
N
Nikita Shchypylov2016-10-17 12:10:25
JavaScript
Nikita Shchypylov, 2016-10-17 12:10:25

How to change icons of all markers in Google Maps API?

This is my google code:

function initMap() {
  var firstMarker = {lat: 50.461686, lng:  30.496442};
  var secondMarker = {lat: 50.430365, lng:  30.520543};
  var thirdMarker = {lat: 50.452029, lng:  30.486235};

  // Create a map object and specify the DOM element for display.
  var locations = [
    ['Bondi Beach', firstMarker, 4],
    ['Coogee Beach', secondMarker, 5],
    ['Cronulla Beach', thirdMarker, 3],
  ];

  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 13,
    center: new google.maps.LatLng(50.451756, 30.509776),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });

  var infowindow = new google.maps.InfoWindow();

  var marker, i;

  for (i = 0; i < locations.length; i++) {
    marker = new google.maps.Marker({
      position: new google.maps.LatLng(locations[i][1], locations[i][2]),
      map: map,
      icon: '/html/img/map_marker.png',
    });

    google.maps.event.addListener(marker, 'click', (function(marker, i) {

      // console.log(i)
      return function() {
        infowindow.setContent(locations[i][0]);
        infowindow.open(map, marker);
        console.log(marker);
        if(marker.icon == '/html/img/map_marker.png') {
          marker.setIcon('/html/img/marker_active.png');
          
        }
        else {
          marker.setIcon('/html/img/map_marker.png');
        }
      }
    })(marker, i));
  }
}

This is how this miracle looks like: joxi.ru/82QegD9UzNvd2d
So when you click on an element: joxi.ru/52anO86TaY3nA0
I need it to change to red when clicking on the marker (as in the second screen), and all the rest to change to blue
Now only implemented the first part, but I can't figure out how to change the picture of the rest of the markers to blue.
Task: when clicking on a marker, give THIS marker a red picture, and the other two markers blue.

Thanks

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question