2013-08-06 61 views
0

我有一些簡單的Google地圖代碼,目前在地圖上放置三個標記。標記在具有不同標題和信息窗口的不同位置顯示正確,但設置爲不同的圖標顯示相同。Google Maps API v3除了圖標,標記是不同的,爲什麼?

我看了各種其他帖子,但沒有一個似乎有同樣的問題。代碼如下:

function codeAddressES(){ 
    codeAddress("BN1 3EL","Title1","Here1", "Red"); 
    codeAddress("BN1 4QU","Title2","Here2", "Yellow"); 
    codeAddress("BN1 3DL","Title3","Here3", "Blue"); 
} 


function codeAddress(address,title,ptContent,ptImage) { 
    var imgRed = 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_red.png'; 
    var imgBlue = 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_blue.png'; 
    var imgYellow = 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_yellow.png'; 

    switch (ptImage){ 
    case "Red": 
     image = imgRed; 
     break; 
    case "Blue": 
     image = imgBlue; 
     break; 
    case "Yellow": 
     image = imgYellow; 
     break; 
    } 


    geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
    map.setCenter(results[0].geometry.location); 
    var marker = new google.maps.Marker({ 
     map: map, 
     position: results[0].geometry.location, 
     title: title, 
     icon: image 
    }); 


    google.maps.event.addListener(marker, 'click', function() { 
    var myHtml = '<strong>#' + ptContent+ '</strong><br/>' ; 
    infoWindow.setContent(myHtml); 
    infoWindow.open(map, marker);  

}); 

任何人都可以指向正確的方向嗎?

感謝

戈登

+1

這是一個常見問題。地理編碼是異步的。當回調函數運行時,所有請求都已發送,圖標保留在最後一個值。您可以在geocoder調用中使用函數閉包來修復它。 – geocodezip

+0

您可以在此搜索的結果中找到一些示例/線索:http://stackoverflow.com/search?q=[google-maps-api-3]+geocodeAddress – geocodezip

+0

:-) @geocodezip很多非常感謝採取有時間來解釋和展示一個已經解決這個問題的例子,並且所有這些都在運作。 – user2657244

回答

0

working example (with function closure)

function codeAddressES(){ 
     codeAddress("BN1 3EL","Title1","Here1", "Red"); 
     codeAddress("BN1 4QU","Title2","Here2", "Yellow"); 
     codeAddress("BN1 3DL","Title3","Here3", "Blue"); 
    } 


function codeAddress(address,title,ptContent,ptImage) { 
    var imgRed = 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_red.png'; 
    var imgBlue = 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_blue.png'; 
    var imgYellow = 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_yellow.png'; 

    switch (ptImage){ 
    case "Red": 
     image = imgRed; 
     break; 
    case "Blue": 
     image = imgBlue; 
     break; 
    case "Yellow": 
     image = imgYellow; 
     break; 
    } 
    geocodeAddress(address, title, ptContent,image); 

} 

function geocodeAddress(address,title, ptContent,image) { 
    geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
    map.setCenter(results[0].geometry.location); 
    bounds.extend(results[0].geometry.location); 
    var marker = new google.maps.Marker({ 
     map: map, 
     position: results[0].geometry.location, 
     title: title, 
     icon: image 
    }); 

    google.maps.event.addListener(marker, 'click', function() { 
     var myHtml = '<strong>#' + ptContent+ '</strong><br/>' ; 
     infoWindow.setContent(myHtml); 
     infoWindow.open(map, marker);  
    }); 
    } else alert("Geocode failed, status: "+status); 
    }); 
}