1

我從ajax頁面加載標記和多義線,每個請求數據顯示在索引頁上,現在我要清除數據(標記,折線)。 ..)之前,可以從ajaxPageGoogle Maps API - 從ajax加載新數據之前清除標記,多段線

得到新的數據

索引頁:

var gmarkers = []; 
var map = null; 
function initialize() { 
    var myOptions = { 
    zoom: 15, 
    center: new google.maps.LatLng(35, 53), 
    // mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    google.maps.event.addListener(map, 'click', function() { 
     infowindow.close(); 
     }); 

} 

var infowindow = new google.maps.InfoWindow(
    { 
    size: new google.maps.Size(150,50) 
    }); 

function myclick(i) { 
    google.maps.event.trigger(gmarkers[i], "click"); 
} 

function createMarker(latlng, name, html) { 
    var contentString = html; 
    var marker = new google.maps.Marker({ 
     position: latlng, 
     map: map, 
     zIndex: Math.round(latlng.lat()*-100000)<<5 
     }); 

    google.maps.event.addListener(marker, 'click', function() { 
     infowindow.setContent(contentString); 
     infowindow.open(map,marker); 
     }); 
    // save the info we need to use later for the side_bar 
    gmarkers.push(marker); 
} 

AJAX頁面(record2.php):

var polylines = []; 

var beaches = [ 
    ['Bondi Beach',10,15, 4], 
    ['Coogee Beach',11,16, 5], 
    ['Cronulla Beach',13,15, 3], 
    ['Manly Beach',13,17, 2], 
    ['Maroubra Beach',12,10, 1] 
]; 

for (var i = 0; i < beaches.length; i++) { 
    var beach = beaches[i]; 
    var myLatLng = new google.maps.LatLng(beach[1], beach[2]); 
    // var polylines = new google.maps.LatLng(beach[1], beach[2]); 
    polylines.push(new google.maps.LatLng(beach[1], beach[2])); 

    var marker = createMarker(myLatLng,"This place",beach[0]) 
} 

var routes = new google.maps.Polyline({ 
    path: polylines, 
    strokeColor: "#FF0000", 
    strokeOpacity: 0.6, 
    strokeWeight: 4 
}); 

routes.setMap(map); 

問:簡單的方法來清除之前加載新的數據折線,標記和等來自ajax page

/---編輯---/
我檢查Google Maps API v3: How to remove all markers?的答案,但來自請求的頁面只是第一個請求是響應,下一個請求將無法正常工作,我想我錯了添加一個清晰的地圖功能

呼叫AjaxPage從指數:

$(document).ready(function(){ 
    $('#loader').hide(); 

     $("#search_button").click(function() { 

      $('#loader').fadeIn(200); 
      $('#login_group').slideUp(200); 
      $.post("record2.php", { 
       time: $('#login_username').val() 

      }, function(response){ 
       setTimeout("finishAjax('login_group', '"+escape(response)+"')", 200); 
      }); 
      return false; 
     }); 
    }); 

function finishAjax(id, response){ 
    $('#loader').slideUp(300); 
    $('#'+id).html(unescape(response)); 
    $('#'+id).fadeIn(200); 

} 
+0

地圖有類似的問題在http://stackoverflow.com/questions/2948097/google-maps-api-v3-如何清除覆蓋 – 2012-07-31 06:57:15

+0

@kidmenot我檢查了它,當我使用它的時候((record2.php)的第一個請求是工作,並且接下來的請求與'record2.php'的後續請求不起作用,我想在clearmap中我使用它的問題(在上面的鏈接上)。 – Root125 2012-07-31 08:28:35

回答

6

創建變量的標記陣列。然後創建標記時,添加標記數組。你可以試試這個。

var markerArrays = new Array(); 
for (var i = 0; i < beaches.length; i++) { 
    var beach = beaches[i]; 
    var myLatLng = new google.maps.LatLng(beach[1], beach[2]); 
    // var polylines = new google.maps.LatLng(beach[1], beach[2]); 
    polylines.push(new google.maps.LatLng(beach[1], beach[2])); 

    var marker = createMarker(myLatLng,"This place",beach[0]); 
    markerArrays.push(marker); 
} 

$.each(markerArrays, function(index, val) { 
    val.setMap(null); 
}); 
+0

謝謝,我必須在'index page'或'ajax requested page'中使用上面的代碼嗎? – Root125 2012-08-05 06:41:05

+0

是的,@root。您可以在index.page的代碼上方使用此代碼。 – turankonan 2012-08-06 05:36:34

1

只是重置地圖
即負荷再次 map = new google.maps.Map(document.getElementById('map'), mapOptions);

+0

即使如此:地圖=新google.maps.Map(的document.getElementById( '地圖'),{ \t \t \t中心:map.getCenter(), \t \t變焦:map.getZoom() \t \t}) ; – djdance 2016-12-18 09:42:27

相關問題