检查XML HTTP请求

时间:2016-06-22 13:59:01

标签: javascript html xml google-maps asp-classic

我的任务是为现有应用程序添加功能。

目前它显示了谷歌地图,如果XML HTTP响应中没有代码,则使用坐标的特定区域的叠加将用红色进行颜色编码。然后,如果有值,则将其着色为绿色。

function loadXMLDoc() {
  var xmlhttp;
  if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      stationground = xmlhttp.responseText.split(",");
    }    
  }
  xmlhttp.open("GET","stationareas.asp",true);
  xmlhttp.send();

  console.log(stationground);
}


function areaStatus() {
  loadXMLDoc();
  map.data.setStyle( function(feature) {
    var featurecountry = feature.getProperty('letter');
    if (stationground.indexOf(featurecountry) != -1) {    
      return/** @type {google.maps.Data.StyleOptions} */ {
        fillColor: 'red' , fillOpacity: 0.25 };
    } else {
      return/** @type {google.maps.Data.StyleOptions} */ {
        fillColor: 'green' , fillOpacity: 0.25 }; 
    }

    console.log(featurecountry);
  });
}

console.log返回stationareas.asp中SQL查询中列出的项列表。

areaStatus()函数中是否有可能以某种方式检查响应是否已经存在,如果可以,我们可以计算出是否有3个“Apple”值,然后将该部分的颜色设置为Green。但是如果那个部分“紫色”有> 5颜色。

希望这是有道理的。任何帮助都会非常有帮助!

回应结构:

[“C04”,“C04”,“C09”,“C21”,“C24”,“C26”,“C43”,“C46”,“C46”,“C66”,“C68”,“C21” “,”C09“,”C21“,”C21“,”C21“,”E10“,”E11“,”E13“,”E14“,”E20“,”E20“,”E22“,”E26“, “G10”,“G10”,“G10”,“G10”,“G10”,“G10”,“G10”,“G10”,“G23”,“G38”,“G38”,“G60”,“G60 “,”G60“,”G10“,”G10“,”G10“,”G60“,”L15“,”L30“,”L30“,”L30“,”L31“,”L32“,”L32“, “L35”,“L55”,“L55”,“L72”,“L95”,“L30”,“L30”,“L55”,“L30”,“”]

1 个答案:

答案 0 :(得分:2)

您需要对响应数组进行重复数据删除,并计算每个结果的出现次数。

关于在JavaScript中重复数据/计数唯一事件的主题,有许多Stack Overflow搜索结果。这是一种粗略的方法,使用O(n * n)来构建返回对象。

<强>的JavaScript

// update the colors
function updateColors(obj) {
    if(obj.count == 1) {
        obj.fillColor = "red";
    } else if(obj.count < 3) {
        obj.fillColor = "yellow";
    } else if(obj.count < 10) {
        obj.fillColor = "green";
    } else {
        obj.fillColor = "blue";
    }
}

// your test data
var responses = ["C04", "C04", "C09", "C21", "C24", "C26", "C43", "C46", "C46", "C66", "C68", "C21", "C09", "C21", "C21", "C21", "E10", "E11", "E13", "E14", "E20", "E20", "E22", "E26", "G10", "G10", "G10", "G10", "G10", "G10", "G10", "G10", "G23", "G38", "G38", "G60", "G60", "G60", "G10", "G10", "G10", "G60", "L15", "L30", "L30", "L30", "L31", "L32", "L32", "L35", "L55", "L55", "L72", "L95", "L30", "L30", "L55", "L30", ""];

// a new array to track the objects
var results = [];

// for every element in your response
for (var i = 0; i < responses.length; i++) {

    // see if there is an existing match
    var found = false;
    // loop over the existing results
    for( var j = 0; j < results.length; j++ ) {
        // if the current response matches an existing result, update the count
        if(results[j].name == responses[i]) {
            results[j].count++;
            updateColors(results[j]); //update the colors
            found = true; //set the flag to true, so we dont add this as a new result
            j = results.length; //exit the loop
        }
    }
    // if the response element wasnt matched, add it as a new result
    if(!found) {
        results.push({name: responses[i], count: 1, fillColor: 'red', fillOpacity: 0.25})
    }   
}

//print everything
console.log(results)

&#39;结果[2]&#39;

的输出示例
  数:5   fillColor:&#34;绿色&#34;
  fillOpacity:0.25
  名称:&#34; C21&#34;

你可以看到它在这个JS小提琴中工作:https://jsfiddle.net/igor_9000/8tbmw2fg/

希望有所帮助!