从地图中删除标记

时间:2015-01-15 05:52:00

标签: javascript google-maps

我只是想删除默认标记,并在点击按钮时替换我在地图上的标记。当我点击按钮标记被移除并成功放置我的标记时,它只发生一次,我想再次点击该按钮,我的标记位置会显示默认标记。

function dropMarkers(flag,id) {
 if(flag == 0)
 {
      for (var i = 0; i < neighborhoods.length; i++) {
        setTimeout(function() {
          addMarker(flag,id);
        }, i * 200);
      }
  }
  else if(flag == 1){
     for (var i = 0; i < 1; i++) {
        setTimeout(function() {
          addMarker(flag,id);
        }, i * 200);
      }
  }
  else if(flag == 2){
     for (var i = 0; i < 1; i++) {
        setTimeout(function() {
          addMarker(flag,id);
        }, i * 200);
      }
  }
}
    function addMarker(flag,markerId) {
        if(flag == 0)
        {
            var marker = new google.maps.Marker({
                position: neighborhoods[uniqueId],
                map: map,
                title: 'uniqueId.>'+uniqueId,
                draggable: false,
                animation: google.maps.Animation.DROP
            });
            marker.id = uniqueId;
            jQuery( ".drop1" ).append("<input type = 'button'  onclick = 'DeleteMarker(" + marker.id + ","+ flag +");' value = 'Delete"+marker.id+"Marker' class='button_"+marker.id+"' />");
            uniqueId++;
            markers.push(marker);

        }
        else if(flag == 1){
            var marker = new google.maps.Marker({
                position: neighborhoods[markerId],
                map: map,
                title: 'uniqueId.>'+markerId,
                icon:'http://192.241.197.114/wp-content/themes/Divi/images/mappin/favorite-pin.png',
                draggable: false,
                animation: google.maps.Animation.DROP
            });
            marker.id = markerId;
            //jQuery( ".drop1" ).append("<input type = 'button'  onclick = 'DeleteMarker(" + marker.id +","+ flag + ");' value = 'Delete"+markerId+"Marker' class='button_"+marker.id+"' />");
            markers.push(marker);
        }
        else if(flag == 2)
        {
            var marker = new google.maps.Marker({
                position: neighborhoods[markerId],
                map: map,
                title: 'uniqueId.>'+markerId,
                draggable: false,
                animation: google.maps.Animation.DROP
            });

            marker.id = markerId;
            //jQuery( ".drop1" ).append("<input type = 'button'  onclick = 'DeleteMarker(" + marker.id +","+ flag + ");' value = 'Delete"+marker.id+"Marker' class='button_"+marker.id+"' /><br />");
            markers.push(marker);
        }
    }
    function DeleteMarker(id,flag) {
     if(flag == 0){
        //Find and remove the marker from the Array
            for (var i = 0; i < markers.length; i++) {
                if (markers[i].id == id) 
                {
                    //Remove the marker from Map                   
                    markers[i].setMap(null);

                    //Remove the marker from array.
                    //markers.splice(i, 1);
                    jQuery('.button_'+id).hide();
                    dropMarkers(1,id);
                    return;
                }
            }
        }
        else if(flag == 1){
            for (var i = 0; i < markers.length; i++) {
                if (markers[i].id == id) 
                {
                    //Remove the marker from Map                   
                    markers[i].setMap(null);
                    markers.splice(i, 1);
                    //Remove the marker from array.
                    //markers.splice(i, 1);
                    jQuery('.button_'+id).hide();
                    dropMarkers(2,id);
                    return;
                }
            }
        }
        else if(flag == 2){
            for (var i = 0; i < markers.length; i++) {
                if (markers[i].id == id) 
                {
                    //Remove the marker from Map                   
                    markers[i].setMap(null);

                    //Remove the marker from array.
                    //markers.splice(i, 1);
                    jQuery('.button_'+id).hide();
                    dropMarkers(1,id);
                    return;
                }
            }
        }
    }

0 个答案:

没有答案