Validate a variable according to the value

Asked

Viewed 67 times

0

I am with the following problem, I am trying to assemble a structure that validates when the value is <> 0, If <> of 0, it must go mounting variable by variable, when = 0, it must close the structure and finish. Here is an example of more or less how it would be. Ire posting all the code for better understanding, follows:

     <script>
  var customLabel = {
    irrigando: {
      label: 'I'
    },
    plantando: {
      label: 'P'
    }
  };

    function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
      center: new google.maps.LatLng(-9.80300419, -36.07060969),
      center: new google.maps.LatLng(-9.807158, -36.072932),
      center: new google.maps.LatLng(-9.813279, -36.076273),

      zoom: 14,
      scaleControl: true,
      streetViewControl: true,
      style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
        mapTypeIds: ['roadmap', 'terrain'],
      streetViewControlOptions: {
      position: google.maps.ControlPosition.LEFT_TOP
    },
    zoomControl: true,
    zoomControlOptions: {
    position: google.maps.ControlPosition.LEFT_TOP
},
    });
    var infoWindow = new google.maps.InfoWindow;
    // Define the LatLng coordinates for the polygon's path.



      // Change this depending on the name of your PHP or XML file
      downloadUrl('emitixml.php', function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName('marker');
        Array.prototype.forEach.call(markers, function(markerElem) {
          var name = markerElem.getAttribute('name');
          var address = markerElem.getAttribute('address');
          var situacao = markerElem.getAttribute('situacao');
          var type = markerElem.getAttribute('type');
          var cor = markerElem.getAttribute('cor');
          var marcadores1 = parseFloat(markerElem.getAttribute('lat1'));
          var marcadores2 = parseFloat(markerElem.getAttribute('lng1'));
          var marcadores3 = parseFloat(markerElem.getAttribute('lat2'));
          var marcadores4 = parseFloat(markerElem.getAttribute('lng2'));
          var marcadores5 = parseFloat(markerElem.getAttribute('lat3'));
          var marcadores6 = parseFloat(markerElem.getAttribute('lng3'));
          var marcadores7 = parseFloat(markerElem.getAttribute('lat4'));
          var marcadores8 = parseFloat(markerElem.getAttribute('lng4'));
          var marcadores9 = parseFloat(markerElem.getAttribute('lat5'));
          var marcadores10 = parseFloat(markerElem.getAttribute('lng5'));
          var marcadores11 = parseFloat(markerElem.getAttribute('lat6'));
          var marcadores12 = parseFloat(markerElem.getAttribute('lng6'));
          var point = new google.maps.LatLng(
              parseFloat(markerElem.getAttribute('lat')),
              parseFloat(markerElem.getAttribute('lng')));


      var triangleCoords = [ <> 0 ? {lat: marcadores1, lng: marcadores2},
                             <> 0 ? {lat: marcadores3, lng: marcadores4},
                             <> 0 ? {lat: marcadores5, lng: marcadores6},
                             <> 0 ? {lat: marcadores7, lng: marcadores8},
                             <> 0 ? {lat: marcadores9, lng: marcadores10},
                             <> 0 ? {lat: marcadores11, lng: marcadores12},

                           ];

           var bermudaTriangle = new google.maps.Polygon({
           paths: triangleCoords, 
           strokeColor: cor,
           strokeOpacity: 0.8,
           strokeWeight: 2,
           fillColor: cor,
           fillOpacity: 0.35
           }); 

            bermudaTriangle.setMap(map);



          var infowincontent = document.createElement('div');
          var strong = document.createElement('strong');
          strong.textContent = name
          infowincontent.appendChild(strong);
          infowincontent.appendChild(document.createElement('br'));

          var text = document.createElement('text');
          text.textContent =  address
          infowincontent.appendChild(text);

          var text = document.createElement('text');
          text.textContent =  situacao
          infowincontent.appendChild(text);

          var icon = customLabel[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            label: icon.label
          });

          marker.addListener('click', function() {
            infoWindow.setContent(infowincontent);
            infoWindow.open(map, marker);
          });
        });
      });

    }



  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() {}
</script>
  • Can you give an example of the result you want to get? (and by the way this is Javascript, no PHP, right?)

  • I don’t understand much javascript, but wouldn’t it be more practical to use a function to check this? And instead of <> use !=?

  • Hello Sergio, this is the complete code. In triangleCoords I want to assemble the structure when the value is non-zero. More I’m not getting. You’re making a mistake.

  • Good you put the rest of the code. Please answer my question: "You can give an example of the result you want to get?"

  • Yes, in the XML I am reading, I have between 1 and 100 markers, they will form a polygon, have polygon with 4 markers and have others with much more, however when the marker is = 0, I would like it to close the structure '];' The result would be this: var triangleCoords = [ {lat: -9.800981, lng: -36.070569}, {lat: -9.801724, lng: -36.068675}, {lat: -9.805605, lng: -36.070785}, {lat: -9.804552, lng: -36.072524} //{lat: -9.800981, lng: -36.070569} ];

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.