-1

オブジェクト指向のJavaScriptに取り組んでいるので、1つのマップオブジェクトを作成しました。以前のすべてのルートとマーカーを維持する必要があるため、新しいマップオブジェクトを作成していません。私のコードは次のとおりです

function map() {

    this.defaultMapOption   =    {
        center : new google.maps.LatLng(0, 0),
        zoom : 1,
        mapTypeId : google.maps.MapTypeId.ROADMAP
    };

    this.map    =   null;

    this.marker =   [];

    this.directionsDisplay = null;
}

map.prototype.init = function() {


    this.map    =   new google.maps.Map(document.getElementById("map"), this.defaultMapOption);

    var map1    =   this.map;
    var marker1 =   this.marker;

    var count   =   0;

    google.maps.event.addListener(this.map, 'click', function(event) {

        count++;

        $(".tabNavigation li").find("a[href=markers]").trigger('click');

        if ( marker1[count] ) {
            marker1[count].setPosition(event.latLng);
        }
        else
        {
            marker1[count] = new google.maps.Marker({
                position: event.latLng,
                map: map1,
                draggable : true
            });

            //by clicking double click on marker, marker must be removed.
            google.maps.event.addListener(map.marker[count], "dblclick", function() {

                console.log(map.marker);

                map.marker[count].setMap(null);//this was working previously
                map.marker[count].setVisible(false);//added this today

                $("#markers ul li[rel='"+count+"']").remove();

            });

            google.maps.event.addListener(marker1[count], "dragend", function(innerEvent) {
                var geocoder = new google.maps.Geocoder();

                geocoder.geocode({'latLng': innerEvent.latLng}, function(results, status) {

                    if (status == google.maps.GeocoderStatus.OK) {
                        map.addMarkerData(results, count);
                    }
                    else
                    {
                        alert("Geocoder failed due to: " + status);
                    }
                });
            }); 

            var geocoder = new google.maps.Geocoder();

            geocoder.geocode({'latLng': event.latLng}, function(results, status) {

                if (status == google.maps.GeocoderStatus.OK) {

                    map.addMarkerData(results, count);
                }
                else
                {
                    alert("Geocoder failed due to: " + status);
                }
            });
        }
    });

}

上記のコードのみが1つのマーカーに対してのみ機能します。ダブルクリックで初めてマーカーが削除されることを意味します。その後は動作しません。

なぜそれが機能しなくなったのかというアイデア!

4

2 に答える 2

1

カウント値は新しいマーカーで増加しておりaddListener(map.marker[count],...)、カウントには最新の値が含まれます。したがって、そのマーカーのみが削除されます。

countしたがって、addListener関数の最後で値をデクリメントする必要があります。

于 2012-09-17T06:45:56.383 に答える
0

マーカー[1]を追加していますが、マーカー[0]を削除しようとしています。count++;関数の最初から最後に移動します。

于 2012-09-17T06:25:48.083 に答える