E
E
Ekaterina Kononovich2017-03-25 16:45:09
Google
Ekaterina Kononovich, 2017-03-25 16:45:09

How to set markers for a specific map in google maps?

Several Google maps are connected on the page, no more than two markers can be placed on each. Cards are added by the user, so the number is not known in advance. I figured out how to add markers to a specific map, but I can’t figure out how to get markers for only a specific map later. Here is an example code:

var map = Array();
var directionsDisplay = Array();
var markers = Array();

function handleApiReady(map_id) {
  var myLatlng = new google.maps.LatLng(56.323678, 44.0);
  var myOptions = {
    zoom: 12,
    disableDefaultUI: true,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  
  map[map_id] = new google.maps.Map(document.getElementById("waymap_"+map_id+""), myOptions);	
  
  directionsDisplay[map_id]=new google.maps.DirectionsRenderer();
  map[map_id].addListener('click', function(event) {
    if(markers.length>1){
      deleteMarkers(map_id);
    }
    addMarker(event.latLng,map_id);
  });
}

// Adds a marker to the map and push to the array.
function addMarker(location,map_id) {
  var lat, lng;

  var marker = new google.maps.Marker({
    position: location,
    map: map[map_id]
  });
  lng=marker.getPosition().lng();
  lat=marker.getPosition().lat();
  
  markers.push(marker);
}

I tried to make this line:
if(markers[map_id].length>1){
deleteMarkers(map_id);
}
but an error occurs

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