2017-04-13 4 views
1

トラブルシューティングの手助けが必要です。単一のボタンをクリックすると開いているすべてのプッシュピンは、すべてのプッシュピン情報を開きます。私は、一度に一つの情報ボックスだけを開くことができるように、押しピンがクリックされるたびに1つの押しピンインフォボックスが変わることを望みます。ワンプッシュでクリックするとすべて開く

  // BING MAP Java Script  
      var map = null; 
      var pinid = 0; 
      var arrPinInfobox = []; 
      //Bing V8 start 
      function GetMap() { //LocInfo, Lat, Long 
       var _MapElement = document.getElementById("myMap"); 
       if (_MapElement === null || typeof _MapElement === "undefined") 
        return; 
       if(jQuery("#pagesitemap_4_noMap").length < 0) 
        return; 
       var arrLocInfoRec = []; 
       var arrLLAdder = []; 
       var MapCenterLat; 
       var MapCenterLong; 
       var ZoomFactor; 
       var ZipLLSource = jQuery("#hdnZipLL").val(); 
       var LocInfo = jQuery("#hdnCompleteLocInfo").val(); 
       var ZipLL = []; 
       var Lat = ""; 
       var Long =""; 
       console.log("Long"); 
       if(typeof LocInfo === "undefined") 
       { 
        console.log("locInfo Undefined"); 
         return; 
       } 
       if (ZipLLSource.length > 0) { 
        ZipLL = (ZipLLSource).split("`"); 
       } 
       if (LocInfo.length > 0) { 
        arrLocInfoRec = LocInfo.split("|") 
       } 
       if (Lat.length > 0 && Long.length > 0) { 
        MapCenterLat = parseFloat(Lat); 
        MapCenterLong = parseFloat(Long); 
        ZoomFactor = 11; //16 
       } 
       else if (ZipLL.length >= 2) { 
        MapCenterLat = parseFloat(ZipLL[0]); 
        MapCenterLong = parseFloat(ZipLL[1]); 
        ZoomFactor = 11; 
       } 
       var mapOptions = { 
        credentials: '      ', 
        center: new Microsoft.Maps.Location(MapCenterLat, MapCenterLong), 
        mapTypeId: Microsoft.Maps.MapTypeId.Automatic, 
        zoom: ZoomFactor, 
        showScalebar: true 
       } 
       map = new Microsoft.Maps.Map('#myMap', mapOptions); 
       var arrPins = []; 
       var arrPinCenter = []; 
       //Generating Pins for multiple locations with Lat,Long 
       for (var locNum = 0; locNum <= arrLocInfoRec.length - 1; locNum++) { 
        try { 
         arrLLAdder = arrLocInfoRec[locNum].split("`"); 
         if (arrLLAdder.length >= 13) { 
          //var latlong = arrLLAdder[11].split(','); 
          arrPinCenter[locNum] = new Microsoft.Maps.Location(parseFloat(arrLLAdder[11]), parseFloat(arrLLAdder[12])); 
          arrPinCenter[locNum] = new Microsoft.Maps.Location(parseFloat(arrLLAdder[11]), parseFloat(arrLLAdder[12])); 
          arrPins[locNum] = new Microsoft.Maps.Pushpin(
                      arrPinCenter[locNum], { 
                            text: arrLLAdder[8] , 
                            icon: 'https://www.bingmapsportal.com/Content/images/poi_custom.png', 
                            anchor: new Microsoft.Maps.Point(12, 39) 
                            } 
                     ); 
          var adder = arrLLAdder[2] + '\r\n' + arrLLAdder[4] + '\r\n' + arrLLAdder[6] + arrLLAdder[9] + "\r\n" + arrLLAdder[1] 
          // Create the infobox for the pushpin 
          arrPinInfobox[locNum] = new Microsoft.Maps.Infobox(arrPins[locNum].getLocation(), 
           { width: 350, 
            height: 100, 
            title: arrLLAdder[5], 
            description: adder, 
            offset: new Microsoft.Maps.Point(-3,13), 
            visible: false 
           });  
          // Add handler for the pushpin click event.      
          Microsoft.Maps.Events.addHandler(arrPins[locNum], 'click', displayInfobox); 
          // Add the Push Pins and InfoBox to the map all at once       
          if(arrPins.length > 0) { 
           map.entities.push(arrPins); //[locNum] 
          } 
         } 
         else { 
          console.log("Invalid Data: arrLocInfoRec[" + locNum + "] = \"" + arrLocInfoRec[locNum] + "\""); 
         } 
        } catch (e) { 
         console.log(e.message + "\r\n" + arrLocInfoRec[locNum]); 
        } 
       }  
      } 
      function displayInfobox(e) { 
       //map.entities.push(arrPinInfobox); 
       console.log("DisplayBox"); 
       for(var i in arrPinInfobox){ 
       arrPinInfobox[i].setOptions({ visible: true }); 
       arrPinInfobox[parseInt(e.target.getText()) - 1].setOptions({ visible: true }); 
       var infobox = arrPinInfobox[i]; 
       infobox.setMap(map); 
       } 
      } 

答えて

0

displayInfobox関数のコードは、すべてのinfoboxesをループし、真のマップに追加するに見える設定します。あなたのコードは書かれた方法で機能しています。

あなたがしたいことは、あなたの情報ボックスを除外することです。個人的に私は、infoboxのアイデアの全体の配列を嫌う、それは乱雑です。私は、単一のInfoboxを作成し、押しピンがクリックされたときに再利用するというアイディアの前にお勧めしたと思います。これは、一度に1つのインフォボックスしか表示されない場合に最適なアプローチです。一度に複数の情報ボックスを表示できるようにしたい場合は、インフォボックスへの参照をプッシュピンにいくつか保存します。 Bing Mapsのすべての図形には、カスタムデータ用に予約されたメタデータプロパティがあります。また、マップに複数回押しピン配列を追加すると、問題が発生することに気付きました。ここで私は//リッキーとのコメントを追加しました、あなたのコードへの変更案である:私が行った変更を示すために:

// BING MAP Java Script  
var map = null; 
var pinid = 0; 
var arrPinInfobox = []; 
//Bing V8 start 
function GetMap() { //LocInfo, Lat, Long 
    var _MapElement = document.getElementById("myMap"); 
    if (_MapElement === null || typeof _MapElement === "undefined") 
     return; 
    if(jQuery("#pagesitemap_4_noMap").length < 0) 
     return; 
    var arrLocInfoRec = []; 
    var arrLLAdder = []; 
    var MapCenterLat; 
    var MapCenterLong; 
    var ZoomFactor; 
    var ZipLLSource = jQuery("#hdnZipLL").val(); 
    var LocInfo = jQuery("#hdnCompleteLocInfo").val(); 
    var ZipLL = []; 
    var Lat = ""; 
    var Long =""; 
    console.log("Long"); 
    if(typeof LocInfo === "undefined") 
    { 
     console.log("locInfo Undefined"); 
      return; 
    } 
    if (ZipLLSource.length > 0) { 
     ZipLL = (ZipLLSource).split("`"); 
    } 
    if (LocInfo.length > 0) { 
     arrLocInfoRec = LocInfo.split("|") 
    } 
    if (Lat.length > 0 && Long.length > 0) { 
     MapCenterLat = parseFloat(Lat); 
     MapCenterLong = parseFloat(Long); 
     ZoomFactor = 11; //16 
    } 
    else if (ZipLL.length >= 2) { 
     MapCenterLat = parseFloat(ZipLL[0]); 
     MapCenterLong = parseFloat(ZipLL[1]); 
     ZoomFactor = 11; 
    } 
    var mapOptions = { 
     credentials: '      ', 
     center: new Microsoft.Maps.Location(MapCenterLat, MapCenterLong), 
     mapTypeId: Microsoft.Maps.MapTypeId.Automatic, 
     zoom: ZoomFactor, 
     showScalebar: true 
    } 
    map = new Microsoft.Maps.Map('#myMap', mapOptions); 
    var arrPins = []; 
    var arrPinCenter = []; 

    //Generating Pins for multiple locations with Lat,Long 
    for (var locNum = 0; locNum <= arrLocInfoRec.length - 1; locNum++) { 
     try { 
      arrLLAdder = arrLocInfoRec[locNum].split("`"); 
      if (arrLLAdder.length >= 13) { 
       //var latlong = arrLLAdder[11].split(','); 
       arrPinCenter[locNum] = new Microsoft.Maps.Location(parseFloat(arrLLAdder[11]), parseFloat(arrLLAdder[12])); 

       arrPins[locNum] = new Microsoft.Maps.Pushpin(arrPinCenter[locNum], { 
             text: arrLLAdder[8] , 
             icon: 'https://www.bingmapsportal.com/Content/images/poi_custom.png', 
             anchor: new Microsoft.Maps.Point(12, 39) 
             }); 

       var adder = arrLLAdder[2] + '\r\n' + arrLLAdder[4] + '\r\n' + arrLLAdder[6] + arrLLAdder[9] + "\r\n" + arrLLAdder[1] 
       // Create the infobox for the pushpin 
       //Ricky: Add your infobox as a reference in your pushpin 
       arrPins[locNum]. metadata = new Microsoft.Maps.Infobox(arrPins[locNum].getLocation(), 
        { width: 350, 
         height: 100, 
         title: arrLLAdder[5], 
         description: adder, 
         offset: new Microsoft.Maps.Point(-3,13), 
         visible: false 
        });  

       // Add handler for the pushpin click event.      
       Microsoft.Maps.Events.addHandler(arrPins[locNum], 'click', displayInfobox);    
      } 
      else { 
       console.log("Invalid Data: arrLocInfoRec[" + locNum + "] = \"" + arrLocInfoRec[locNum] + "\""); 
      } 
     } catch (e) { 
      console.log(e.message + "\r\n" + arrLocInfoRec[locNum]); 
     } 
    } 

    // Add the Push Pins and InfoBox to the map all at once       
    //Ricky: Moved this out of the array as you only need to add array of pushpins to the map once. 
    if(arrPins.length > 0) { 
     map.entities.push(arrPins); //[locNum] 
    } 
} 
function displayInfobox(e) { 
    //map.entities.push(arrPinInfobox); 
    console.log("DisplayBox"); 

    //Get infobox from the pushpin, rather than looping through array. 
    var infobox = e.target.metadata; 
    infobox.setOptions({ visible: true }); 

    //for(var i in arrPinInfobox){ 
     //arrPinInfobox[i].setOptions({ visible: true }); 
     //arrPinInfobox[parseInt(e.target.getText()) - 1].setOptions({ visible: true }); 
     //var infobox = arrPinInfobox[i]; 
     //infobox.setMap(map); 
    //} 
} 

をあなたのコードをクリーンアップしたい場合は、いくつかのより多くの私がお勧め:

  • すべての配列を取り除くと、その必要はありません。
  • プッシュピンにレイヤーを使用します。レイヤーごとに1つのクリックイベントを追加するのではなく、個々のプッシュピンごとに追加します。
関連する問題