隐藏Google地图指针的空白地址行

时间:2011-06-29 11:08:17

标签: javascript html google-maps

我有一些指针,看起来很好。在弹出窗口中,我将在javascript中提取各种信息。

var address1 = markers[i].getAttribute("address1");
      var address2 = markers[i].getAttribute("address2");
      var address3 = markers[i].getAttribute("address3");
      var address4 = markers[i].getAttribute("address4");
      var county = markers[i].getAttribute("county");
      var postcode = markers[i].getAttribute("postcode");
      var name = markers[i].getAttribute("name");
      var html = "<b>"+name+"<\/b><p>"
      + address1 + "<br />"
+ address2 + "<br />"
+ address3 + "<br />"
+ address4 + "<br />" 
+ county + "<br />" 
+ postcode + "<br />";

然而,有时候这些行是空白的(如果我想添加更多信息,这将更多应用) - 如何抑制代码中的空白行?这样弹出窗口就会显示数据库中的数据。

感谢。

2 个答案:

答案 0 :(得分:1)

我首先在PHP中构建HTML var内容。像这样:

$address = array();
$address[] = $address1;
$address[] = $address2;
$address[] = $address3;
$address[] = $address4;
$address[] = $county;
$address[] = $postcode;

array_filter($address); // this will remove blank entries
$address = implode("<br />",$address);

否则你需要在javascript中做类似的事情,这可能有点棘手。

确定javascript版

var address = [];
address.push(markers[i].getAttribute("address1"));
address.push(markers[i].getAttribute("address2"));
address.push(markers[i].getAttribute("address3"));
address.push(markers[i].getAttribute("address4"));
address.push(markers[i].getAttribute("county"));
address.push(markers[i].getAttribute("postcode"));

for ( var i=0; i < address.length; i++) {
    if ( address[i].length == 0 ) address.splice(i,1);
}
var html = address.join('<br />');

我认为这应该有用,对不起,我还没有测试过。

答案 1 :(得分:0)

虽然以上可能有用(尚未测试,但会做),我找到了

的解决方案
  if (address1 != "") {
       var address1a = address1 + "<br\/>";
    } else {
address1a = "";
}
    if (address2 != "") {
       var address2a = address2 + "<br\/>";
    } else {
address2a = "";
}
    if (address3 != "") {
       var address3a = address3 + "<br\/>";
    } else {
address3a = "";
}
    if (address4 != "") {
       var address4a = address4 + "<br\/>";
    } else {
address4a = "";
}
    if (county != "") {
       var countya = county + "<br\/>";
    } else {
countya = "";
}
    if (postcode != "") {
       var postcodea = postcode + "<br\/>";
    } else {
postcodea = "";
}

          var html = "<b>"+ name + "<\/b><br\/>" + address1a + address2a + address3a + address4a + countya + postcodea + "<br\/>";

甚至可能是一种更简单的方法,但它确实有效:)