Set mark on google maps

1

I am working on a project that reads a database and adds marks on a map (these marks are variables that change every 20 seconds). The problem is that it only shows me the last brand I make, and not all.

I know I have to do new google.maps.Marker() for each of them, but I do not know how to do it because I'm very new working with HTML + JavaScript. How could I do it?

These are two links to a web page where you can see the problem: link 1 and link 2 ; and this is the code that I have so far:

//<![CDATA[

var customIcons = {
  '2': { icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png' },
  '1': { icon: 'http://labs.google.com/ridefinder/images/mm_20_green.png' },
  '0': { icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png' },
  '3': { icon: 'http://labs.google.com/ridefinder/images/mm_20_yellow.png' }
};

function initMap() {

  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(7.109176, -73.114219),
    zoom: 14,
    mapTypeId: 'roadmap'
  });
  var geocoder = new google.maps.Geocoder();
  var infoWindow = new google.maps.InfoWindow;
  var lati = null;
  var lgd = null;
  var marker = new google.maps.Marker({
    map: map,
    draggable:true
  });

  var markr = new google.maps.Marker({
    map: map,
  });

  function Actualiza (){

    // Change this depending on the name of your PHP file
    downloadUrl("Consulta.php", function(data) {  
      var xml = data.responseXML;
      var markers = xml.documentElement.getElementsByTagName("marker");
      for (var i = 0; i < markers.length; i++) {
        var name = markers[i].getAttribute("name");
        var address = markers[i].getAttribute("address");
        var cedul = markers[i].getAttribute("cedul");
        var type = markers[i].getAttribute("type");
        var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
        var html = "<b>" + name + "</b> <br/>" + address + "</b> <br/>" + cedul;
        var icon = customIcons[type] || {};
        markr.setPosition(point);
        markr.setIcon(icon.icon);
        console.log(point);
        bindInfoWindow( markr, map, infoWindow, html);
      }
    });
  }

  document.getElementById('submit').addEventListener('click', function() {
    geocodeAddress(geocoder, map);
  });
  setInterval(Actualiza, 10000);

  document.getElementById('enviar').addEventListener('click', function() {
    var cedu = $('#ce').val();
    var comen= $('#co').val();
    var loo = $('#long').val();
    var laa = $('#lat').val();
    var settings = {
      "async": true,
      "crossDomain": true,
      "url": "comentario.php",
      "method": "POST",
      "headers": {
        "content-type": "application/json",
        "cache-control": "no-cache",
        "postman-token": "d2df5b1a-1820-68be-fa45-34ac55d9697b"
      },
      "processData": false,
      "data": "{\"username\":\""+cedu+"\",\"comentario\":\""+comen+"\",\"servicio\":\"1\",\"lat\":\""+laa+"\",\"lon\":\""+loo+"\"}"
    }

    $.ajax(settings).done(function (response) {
      console.log(response);
      console.log(settings)
    });
  });

  function geocodeAddress(geocoder, resultsMap) {
    var address = document.getElementById('address').value;
    geocoder.geocode({'address': address}, function(results, status) {
      if (status === google.maps.GeocoderStatus.OK) {
        resultsMap.setCenter(results[0].geometry.location);
        marker.setPosition(results[0].geometry.location);
        updatePosition(results[0].geometry.location);
      } else {
        alert('No podemos encontrar la dirección' + status);
      }
      console.log(results[0].geometry.location)
      google.maps.event.addListener(marker, 'dragend', function(){
        updatePosition(marker.getPosition());
      });
    });
  }
}

function bindInfoWindow(markr, map, infoWindow, html) {
  google.maps.event.addListener(markr, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, markr);
  });
}

function downloadUrl(url, callback) {
  var request = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
}



function doNothing() {}

//]]>
<div id="floating-panel">
  <input id="address" type="textbox" value="Bucaramanga">
  <input id="submit" type="button" value="Buscar">
</div>
<div id="map"></div>
<div id="floatin">
  <input type="text" name="lat" id="lat" placeholder="Latitud">
  <input type="text" name="lng" id="long" placeholder="Longitud">
  <input type="textbox" name="placa" id="ce" placeholder="Cedula">
  <input type="textbox" name="comentarios" id="co" placeholder="Comentarios">
  <input id="enviar" type="button" value="Enviar">
</div>

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA_1sJiE-QWC_mvdIqJB2eFvIPt6w&signed_in=true&callback=initMap" async defer></script>
    
asked by David Peña 05.09.2016 в 00:04
source

1 answer

0

Solved. simply force the marks to enter them in a vector, show them and then delete them.

  function clearMarkers() {
          for (var j = 0; j < gmarkers.length; j++) {
            gmarkers[j].setMap(null);
          }
          gmarkers.length = 0;
        }

      function addmarcas (point,icon){
        markrs = new google.maps.Marker({
        map: map,
        position:point,
        icon:icon
      });
      gmarkers.push(markrs);
      }'
    
answered by 08.09.2016 / 23:38
source