0

我有一些简单的谷歌地图代码,目前在地图上放置了三个标记。标记在具有不同标题和信息窗口的不同位置都显示正确,但是为每个设置不同的图标都显示相同。

我看过其他各种帖子,但似乎没有一个有同样的问题。代码如下:

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);     

});

谁能指出我正确的方向?

谢谢

戈登

4

1 回答 1

0

工作示例(带函数闭包)

    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);
  });
}
于 2013-08-06T16:37:00.123 回答