Google地图自定义标记图标,用于显示不同的标记

时间:2013-12-26 02:34:36

标签: google-maps google-maps-api-3 google-maps-markers

我有一系列自定义图标:

 var iconBase = 'http://localhost/icons/';

 var icons = {
 red: {
    icon: iconBase + 'red.png'
  },
  green: {
    icon: iconBase + 'green.png'
  },
  info: {
    icon: iconBase + 'red.png'
  }
};

现在,当我在Marker中将它们称为icon: icons[red].icon, at:

var marker = new google.maps.Marker({
  position: latlng,
  icon: icons[red].icon,
  map: map
});

我没有在地图上找到任何标记你能告诉我我做错了什么吗?这是整个代码:

var map;
$(document).ready(function () {
        var latlng = new google.maps.LatLng(49.241943, -122.889318);
        var myOptions = {
                        zoom: 17,
                        center: latlng,
                        disableDefaultUI: true,
                        mapTypeId: google.maps.MapTypeId.ROADMAP,
                        scaleControl: true,
                        panControl: true,
                        mapTypeControl:true,
                        mapTypeControlOptions: {
  style:google.maps.MapTypeControlStyle.DROPDOWN_MENU,
  position:google.maps.ControlPosition.TOP_CENTER
},
                        zoomControl: true
        };
       map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
 //Icon Contoling
     var iconBase = 'http://localhost/icons/';
    var icons = {
  red: {
    icon: iconBase + 'red.png'
  },
  green: {
    icon: iconBase + 'green.png'
  },
  info: {
    icon: iconBase + 'red.png'
  }
};

var marker = new google.maps.Marker({
  position: latlng,
  icon: icons[red].icon,
  map: map
});

});

1 个答案:

答案 0 :(得分:1)

ReferenceError:未定义红色

尝试更改

icon: icons[red].icon

icon: icons['red'].icon