3

Googleマップのマーカーがクリックされたときに何かを起こす必要があります。このリンクのデモを出発点として使用しています。

http://gmap3.net/examples/tags.html

更新-次のことを試しました:

$.each(markers, function(i, marker){
  marker.click(function(){  
     alert('alert');
  });
});

$.each(markers, function(i, marker){
  $(marker).click(function(){  
     alert('alert');
  });
});

更新この中間セクションを既存のコードに追加しようとしました:

        $.each(markers, function(i, marker){
            marker.setMap( checked ? map : null);

           //added code begins
            $(marker).click(function(){ 
                 alert('dfd');
              });
           //added code ends

        });
4

3 に答える 3

3
google.maps.event.addListener(marker, 'click', function() {

});

参照: https://code.google.com/apis/maps/documentation/javascript/events.html#UIEvents

例: http: //jsfiddle.net/diode/yXQwp/

于 2012-02-28T12:53:05.217 に答える
1

OPは、Googleマップレイヤーの上にAPIレイヤーを追加するjQueryプラグインライブラリであるGMAP3ライブラリを使用してこれを行うことを求めていることに注意してください。Google API が google.maps.addListener を使用することであると言うのは絶対に正しいですが、OP はaddMarkers を使用して GMAP3 の例に近いものを望んでいると思いますが、クリック イベントをリッスンします。それを念頭に置いて、GMAP3 の例を修正し、mouseenter イベントを click イベントに変更し、mouseleave イベントを取り除きました。

さらに遊びたい人のために、この例の jsFddleを作成しました。

ソースは次のとおりです。

<!DOCTYPE HTML>
<html>
<head>
    <style>
     #map {
      width: 400px;
      height: 400px;
  }
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">  </script>
<script type="text/javascript" src="http://gmap3.net/js/gmap3-4.1-min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#map').gmap3({
            action: 'init',
            options: {
                center: [46.578498, 2.457275],
                zoom: 5
            }
        }, {
            action: 'addMarkers',
            markers: [
            {
                lat: 48.8620722,
                lng: 2.352047,
                data: 'Paris !'},
                {
                    lat: 46.59433,
                    lng: 0.342236,
                    data: 'Poitiers : great city !'},
                    {
                        lat: 42.704931,
                        lng: 2.894697,
                        data: 'Perpignan ! <br> GO USAP !'}
                        ],
                        marker: {
                            options: {
                                draggable: false
                            },
                            events: {
                                click: function(marker, event, data) {
                                    var map = $(this).gmap3('get'),
                                    infowindow = $(this).gmap3({
                                        action: 'get',
                                        name: 'infowindow'
                                    });
                                    if (infowindow) {
                                        infowindow.open(map, marker);
                                        infowindow.setContent(data);
                                    } else {
                                        $(this).gmap3({
                                            action: 'addinfowindow',
                                            anchor: marker,
                                            options: {
                                                content: data
                                            }
                                        });
                                    }
                                }
                            }
                        }
                    });
});​
</script>
</head>
<body>
    <div id="map" style="width: 400x; height: 400px"></div>​
</body>
</html>

于 2012-03-01T15:22:18.357 に答える
0

あなたの質問にはあまり詳細はありませんが、次のようなものが必要になるかもしれません。

$.each(markers, function(i, marker){
  marker.click(function(){  //maybe, its $(marker).click
     console.log(marker);
  });
});

編集:これはあなたのために働くかもしれません...

google.maps.event.addListener(marker, 'click', toggleBounce);

その正確な行をグーグルで検索すると、さらにポインタが得られます...頑張ってください!

于 2012-02-28T12:01:55.007 に答える