1

Googleマップに複数のマーカーがあり、それらをクリックするとモーダルウィンドウを開く必要があります。問題は、すべてのマーカーに特定の ID があり、各モーダルでその ID が必要なことです。

モーダルhtmlは次のとおりです。

<div class="modal fade" id="imageModal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
      </div>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </div>
</div>

この関数を呼び出す JavaScript コードは次のとおりです。

function createMarker(pos, t, map) {
    var marker = new google.maps.Marker({
        position: pos,
        map: map,  // google.maps.Map
        index: t
    });
    google.maps.event.addListener(marker, 'click', function() {
      $('#imageModal').modal();
      //alert("I am marker " + marker.index);
    });
    return marker;
 }

したがって、imageModal では、動的な marker.index 情報にアクセスする必要があります。どうやってやるの?何か案は ?

4

1 に答える 1

0

まず、Google マップ v3 のMarkerOptionindexではないと思います。

どのようにこれを取得したかtitleというと、MarkerOption でオプションを使用し、動的文字列に設定してから、addListenerイベントでアクセスしました。

方法は次のとおりです。

function createMarker(pos, t, map) {
    var marker = new google.maps.Marker({
        position: pos,
        map: map,  // google.maps.Map
        title: t   //where t must be a string
    });
    google.maps.event.addListener(marker, 'click', function() {
      $('#imageModal').modal();
      alert("I am marker " + this.getTitle() );  
    });
 }

これがお役に立てば幸いです!!

于 2014-01-04T06:26:21.540 に答える