2017-04-13 2 views
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 함수의 코드는 모든 infobox를 반복하고 표시를 true로 설정하고지도에 추가합니다. 코드가 어떻게 작성되었는지 작동합니다.

당신이하고 싶은 것은 당신의 infoboxes를 걸러내는 것입니다. 개인적으로 나는 infobox 아이디어의 전체 배열을 싫어, 그것은 지저분 해. 나는 하나의 infobox를 생성하고 압정이 클릭되었을 때 그것을 재사용한다는 생각보다 먼저 추천했다고 생각한다. 한 번에 하나의 정보 표시가 필요할 경우 최상의 방법입니다. 한 번에 여러 개의 infobox를 보여줄 수 있기를 원하면 infobox에 대한 참조를 압정으로 저장하십시오. 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); 
    //} 
} 

당신이 당신의 코드를 정리하려면 좀 더 내가 추천 :

  • 모든 어레이를 제거하면 필요하지 않습니다.
  • 압정 용 레이어를 사용하십시오. 각 개별 압정보다는 레이어에 단일 클릭 이벤트를 추가하십시오.
관련 문제