T
T
Tvoi_Vaska2019-09-11 10:33:24
JavaScript
Tvoi_Vaska, 2019-09-11 10:33:24

How to find a marker on Google maps by id and activate its InfoWindow?

Google Maps has markers that were imported from a json file. There are buttons that, when clicked, move the window and open an infowindow marker, defined by an id that matches the id of the button from the "selector" class.

<button class="selector" id="1">Маркер 1</button>
<button class="selector" id="2">Маркер 2</button>
<button class="selector" id="3">Маркер 3</button><br />
 <script>
 var map;
 var markers=[];
function initMap() {
self = this;
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 2,
    center: new google.maps.LatLng(2.8,-187.3),
    mapTypeId: 'terrain'
  });
  var script = document.createElement('script');
  script.src = '';
  document.getElementsByTagName('head')[0].appendChild(script);
  
   var infowindow = new google.maps.InfoWindow();
  
  window.eqfeed_callback = function(results) {
  for (var i = 0; i < results.features.length; i++) {
  var place = results.features[i].properties.place;
    var coords = results.features[i].geometry.coordinates;
  var id = results.features[i].properties.ids;
    var latLng = new google.maps.LatLng(coords[1],coords[0]);
    var marker = new google.maps.Marker({
      position: latLng,
      map: map,
      title: 'Нажмите чтобы приблизить',
    description : place,
    id : id,
    });
  markers[i] = {
    position: latLng,
    description: place,
    id: id,
  }
  
  google.maps.event.addListener(marker, 'click', function() {
  map.setZoom(8);
    map.setCenter(this.getPosition());
  infowindow.setContent('<div><strong>' + this.description +'</strong><br></div>');
    infowindow.open(map, this);
});
  }
}


console.log(markers[1[1]]);
}
    </script>

I get an error: Uncaught ReferenceError: markers is not defined
Help to defeat the error.

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