简体   繁体   English

Google地图-自定义标记图标显示在默认图标下

[英]Google Maps - Custom Marker Icon appears under the default icon

I would like to add a marker with a custom icon on the map. 我想在地图上添加带有自定义图标的标记。

 marker = new google.maps.Marker({
   position: coords,
   map: map
 });        
if(attrs.icon)
    marker.setIcon(attrs.icon);

Doing this will add a new marker on the map, but it adds the custom icon under the default icon (red location symbol). 这样做会在地图上添加一个新标记,但会在默认图标(红色位置符号)下添加自定义图标。 This is strange. 这很奇怪。 Any ideas? 有任何想法吗?

Ordinarily, you would add a custom icon using the following: 通常,您将使用以下命令添加自定义图标:

  var image = 'http://i.imgur.com/OG4CZt3.png';
  var beachMarker = new google.maps.Marker({
    position: {lat: -33.890, lng: 151.274},
    map: map,
    icon: image
  });


Here is the full snippet, from the docs 这是完整的摘录, 来自docs

 #map { height: 250px; } 
 <div id="map"></div> <script> // This example adds a marker to indicate the position of Bondi Beach in Sydney, // Australia. function initMap() { var map = new google.maps.Map(document.getElementById('map'), { zoom: 4, center: {lat: -33, lng: 151} }); var image = 'http://i.imgur.com/OG4CZt3.png'; var beachMarker = new google.maps.Marker({ position: {lat: -33.890, lng: 151.274}, map: map, icon: image }); } </script> <script async defer src="https://maps.googleapis.com/maps/api/js?signed_in=false&callback=initMap"></script> 

Doing it this way should prevent the custom icon from appearing beneath the default. 这样做将防止自定义图标出现在默认图标下方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM