2017-03-06 21 views
1

特に、私は、クリックするとGoogleマップの指定された場所に誘導するリンクでいっぱいのドロップダウンメニューを作ろうとしています。私は自分のページに埋め込まれたGoogleマップを取得することができましたが、何らかの理由でドロップダウンが発生するように見えず、自分のコードで問題を見つけることができません。また、ドロップダウンメニューで実際にリンクを作成して、Googleマップ上の選択した場所に移動する方法を理解することも苦労しています。どんな助けもありがとう。私がこれまで持っているコード...初心者が簡単なドロップダウンメニューで苦労している

function myFunction() { 
 
    document.getElementById("myDropdown").classList.toggle("show"); 
 
} 
 

 
window.onclick = function(event) { 
 
    if (!event.target.matches('.dropbtn')) { 
 
    var dropdowns = document.getElementByClassName("dropdown-content"); 
 
    var i; 
 
    for (i = 0; i < dropdowns.length; i++) { 
 
     var openDropdown = dropdowns[i]; 
 
     if (openDropdown.classList.contains('show')) { 
 
     openDropdown.classList.remove('show') 
 
     } 
 
    } 
 
    } 
 
} 
 

 
function initMap() { 
 
    var Columbia = { 
 
    lat: 34.006140, 
 
    lng: -81.037532 
 
    }; 
 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
    zoom: 4, 
 
    center: Columbia 
 
    }); 
 
    var marker = new google.maps.Marker({ 
 
    position: Columbia, 
 
    map: map 
 
    }); 
 
}
.dropbtn { 
 
    background-color: #4caf50; 
 
    color: white; 
 
    padding: 16px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

 
.dropbtn:hover, 
 
.dropbtn:focus { 
 
    background-color: #3e8e41; 
 
} 
 

 
.dropdown { 
 
    position: relative; 
 
    display: inline-block; 
 
} 
 

 
.dropdown-content { 
 
    display: none; 
 
    position: absolute; 
 
    background-color: #f9f9f9; 
 
    min-width: 160px; 
 
    overflow: auto; 
 
    box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); 
 
    z-index: 1; 
 
} 
 

 
.dropdown-content a { 
 
    color: black; 
 
    padding: 12px 16px; 
 
    text-decoration: none; 
 
    display: block; 
 
} 
 

 
.dropdown a:hover { 
 
    background-color: #f1f1f1 
 
} 
 

 
#map { 
 
    height: 400px; 
 
    width: 100%; 
 
}
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB5cD8oVYji2MWCAj8JzrAzQtpBy12W30o&callback=initMap"> 
 
</script> 
 
<h3>Google Maps/Form Assignment</h3> 
 
<div id="map"></div> 
 
<div class="dropdown"> 
 
    <button onclick="myFunction()" class="dropbtn">Select a location</button> 
 
    <div id="myDropdown" class="dropdown-content"> 
 
    <a href="#london">London, England</a> 
 
    <a href="#tokyo">Tokyo, Japan</a> 
 
    <a href="#newyork">New York City, USA</a> 
 
    </div> 
 
</div>
私はあなたが同じプロセスで、2つの異なる機能を追加した参照

+1

1)クラス 'dropdown-conte nt'にはnoneの表示がありますが、 'show'クラスに対して定義されたスタイルがないのでdisplayはnoneになります.2)' document.getElementByClassName'に文法エラーがあり、 'document.getElementsByClassName'に置き換えます。 – nosthertus

+0

私は投稿しますあなたの解決策の答え、私に瞬間を与えて – nosthertus

答えて

0

、あなただけのように私は修正とコードが表示されます1が必要になります。

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <style> 
     .dropbtn { 
      background-color: #4caf50; 
      color: white; 
      padding: 16px; 
      font-size: 16px; 
      border: none; 
      cursor: pointer; 
     } 
     .dropbtn:hover, .dropbtn:focus{ 
      background-color: #3e8e41; 
     } 
     .dropdown{ 
      position: relative; 
      display: inline-block; 
     } 
     .dropdown-content{ 
      display: none; 
      position: absolute; 
      background-color: #f9f9f9; 
      min-width: 160px; 
      overflow: auto; 
      box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
      z-index: 1; 
     } 
     /*Add this rule so the dropdown can be shown*/ 
     .dropdown-content.show{ 
      display: block; 
     } 
     .dropdown-content a { 
      color: black; 
      padding: 12px 16px; 
      text-decoration: none; 
      display: block; 
     } 
     .dropdown a:hover { 
      background-color: #f1f1f1 
     } 
     #map { 
      height: 400px; 
      width: 100%; 
     } 
    </style> 
</head> 
<body> 
    <h3>Google Maps/Form Assignment</h3> 
    <div id="map"></div> 
    <div class="dropdown"> 
     <button class="dropbtn">Select a location</button> 
     <div id="myDropdown" class="dropdown-content"> 
      <a href="#london">London, England</a> 
      <a href="#tokyo">Tokyo, Japan</a> 
      <a href="#newyork">New York City, USA</a> 
     </div> 
    </div> 

    <script> 
     window.onclick = function(event){ 
      var target = event.target; 

      if(target.matches('.dropbtn')){ 
       var dropdowns = document.getElementsByClassName("dropdown-content"); //This returns a HTMLCollection 

       for(i=0; i < dropdowns.length; i++){ 
        // Since dropdowns is a HTMLCollection, extract the node from the collection 
        var dropdown = dropdowns.item(i); 

        // Toggle the dropdown whenever the button is clicked 
        dropdown.classList.toggle("show"); 
       } 
      } 

      if(target.matches(".dropdown-content > a")){ 
       // Get the location name from the hash 
       var location = target.hash.slice(1); 

       // Extract the location map and set it as center 
       map.setCenter(positions[location]); 
      } 
     }; 
     </script> 
     <script> 
      function initMap() { 
       // Set all locations map 
       window.positions = { 
        london: { 
         lat: 51.5013616, 
         lng: -0.1965444 
        }, 
        southCarolina: { 
         lat: 34.006140, 
         lng: -81.037532 
        } 
       }; 

       window.map = new google.maps.Map(document.getElementById('map'), { 
        zoom: 4, 
        center: positions.southCarolina 
       }); 

       // Add marker positions in the map 
       var markers = { 
        london: new google.maps.Marker({ 
         position: positions.london, 
         map: map 
        }), 
        southCarolina: new google.maps.Marker({ 
         position: positions.southCarolina, 
         map: map 
        }) 
       }; 
      } 

    </script> 
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB5cD8oVYji2MWCAj8JzrAzQtpBy12W30o&callback=initMap"> 
    </script> 
</body> 
</html> 

私はあなたのコードとの違いとして何をすべきかをコメントに追加しました。私が持っている:あなたはdocument.getElementsByClassNameのみitemnamedItemある財産及び2の方法としてlengthを持つHTMLCollectionオブジェクトを返します、あなたはgetElementsByClassName

EDITためHTMLCollectionおよびドキュメントのためのドキュメントを見ることができることを知っておく必要があり

また、ドロップダウンアイテムがクリックされたときにマップ位置のビューを処理するセクションコードを追加しました。他の場所でも残り続けるようにlondonの例を追加しました。

関連する問題