谷歌地图API v3标记除了图标不同,为什么?

时间:2013-08-06 15:08:46

标签: google-maps-api-3

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

我查看了其他各种帖子,但似乎都没有相同的问题。代码如下:

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 个答案:

答案 0 :(得分: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);
  });
}