开发者

Hiding blank address lines for a Google map pointer

开发者 https://www.devze.com 2023-03-16 07:56 出处:网络
I have some pointers, which ap开发者_如何学JAVApear fine. In the pop-up I am pulling in various items of information within the javascript.

I have some pointers, which ap开发者_如何学JAVApear fine. In the pop-up I am pulling in various items of information within the javascript.

i.e.

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 />";

However sometimes the lines are blank (which would apply more if I wanted to add further information) - how can I suppress the blank lines in the code? So that the pop-up simply shows the data in the database.

Thanks.


I would build the HTML var content in PHP first. Like this:

$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);

Otherwise you'll need to do something similar in javascript, which is probably a bit more tricky.

OK javascript version

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 />');

I think that should work, sorry I haven't tested it.


Whilst the above may work (have not yet tested, but will do) , I found a solution with

  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\/>";

Probably a simpler way even to the above, but it works :)

0

精彩评论

暂无评论...
验证码 换一张
取 消