2010-04-09 242 views
0

我遇到了谷歌地圖API V3的問題。似乎每次我加載我的頁面時,地圖加載(包括標記),然後它會快速重新加載並刪除所有標記。我錯過了什麼嗎?我究竟做錯了什麼?谷歌地圖api v3刷新標記

下面是一個例子: http://www.PaulPeelen.com/wp-content/uploads/2010/04/SafariScreenSnapz001.mov

這是我的代碼:

<script type="text/javascript"> 
<!-- 
var hash = "{{$sHashLocal}}"; 
var webroot = "{{$webroot}}"; 
var map; 

function initialize() { 
    var latlng = new google.maps.LatLng(59.32045, 18.066902); 
    var myOptions = { 
     zoom: 13, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

    return map; 
} 

function getMarkerInfo (infowindow, rack_id) 
{ 
    // Get all the racks 
    $.ajax({ 
     type: "GET", 
     url: webroot+"ajax/getRack/"+hash+"/"+rack_id, 
     cache: false, 
     dataType: "html", 
     success: function(html) { 
      $("#rack_"+rack_id).html(); 

      infowindow.setContent(html); 
     } 
    }); 
} 

$(document).ready(function() { 
map = initialize(); 



function attachSecretMessage(marker, rack_id) { 
    var infowindow = new google.maps.InfoWindow({ 
     size: new google.maps.Size(50,50) 
    }); 

    google.maps.event.addListener(marker, 'click', function(){ 
     var ret = '<div id="rack_'+rack_id+'" class="rackDiv"><div class="rackDivLoading"><img src="theme/images/loader-small.gif" border="0"/><span>Hämtar data</span></div></div>'; 
     infowindow.setContent(ret); 
     infowindow.open(map,marker); 

     getMarkerInfo(infowindow,rack_id); 
    }); 
} 

function addPlacemark (lat,lng,title, rack_id) { 
    var image = new google.maps.MarkerImage('http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png', 
       new google.maps.Size(32, 32), 
       new google.maps.Point(0,0), 
       new google.maps.Point(0, 32)); 
    var shadow = new google.maps.MarkerImage('http://maps.google.com/mapfiles/shadow50.png', 
       new google.maps.Size(37, 34), 
       new google.maps.Point(0,0), 
       new google.maps.Point(-4, 34)); 

    var location = new google.maps.LatLng(lat,lng); 
    var marker  = new google.maps.Marker({ 
         position: location, 
         map: map, 
         icon: image, 
         shadow: shadow}); 


    marker.setTitle(title); 

    attachSecretMessage(marker, rack_id); 
} 

// Get all the racks 
$.ajax({ 
    type: "GET", 
    url: webroot+"ajax/getRacks/"+hash, 
    cache: false, 
    dataType: "xml", 
    success: function(xml) { 
     // Add the results 
     $(xml).find("station").each(function() { 
      rack_id = $(this).find("rack_id").text(); 
      title = $(this).find("description").text(); 
      longitute = parseFloat($(this).find("longitute").text()); 
      latitude = parseFloat($(this).find("latitude").text()); 

      addPlacemark(latitude, longitute, title, rack_id); 
     }); 
    } 
}); 

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

    addPlacemark (59.32045, 18.066902); 
}); 

// Set size 
setPageSize(); 
}); 
//--> 
</script> 
+0

setPageSize()函數中會發生什麼? – 2010-04-09 21:44:15

+0

//設置中間尺寸 功能setPageSize() { \t //設置設計 \t變種arrPageSizes = ___ getpagesize的(); \t \t var pageHeight = arrPageSizes [1]; \t var footerHeight = $('#footer')。height(); \t \t var mapHeight = pageHeight - footerHeight; \t \t $('#map_canvas')。height(mapHeight); (')調整大小',函數(){ \t setPageSize(); }); 只有那個......但是這個問題在我添加之前就已經發生了。 我很高興知道,在Firefox中它有時可以工作,有時它不會。 – 2010-04-09 21:46:40

+0

另外,如果您使用XHTML,而不是將您的腳本包裝在html註釋中,請嘗試將它們包裝在cdata標籤中。我懷疑這是問題,但它是*首選的方式。請參閱:http://javascript.about.com/library/blxhtml.htm – 2010-04-09 21:49:46

回答

1

您的最後一個職位讓我思考,所以我忽略了我的代碼,看到一個老調試代碼重新運行initialize()函數。 感謝您的幫助!