最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using MarkerWithLabel on Google Maps hide the marker - Stack Overflow

programmeradmin4浏览0评论

I am currently using the google maps marker with label control. I can dynamically assign a marker to the map, and have a label for that marker.

I have figured out how to hide the label however, I am at a bit of a loss on how to hide the icon and show the label. Anyone with some suggestions on this?

markerWithLabel.setMap(null);

removes the marker and label, not just the marker.

I am currently using the google maps marker with label control. I can dynamically assign a marker to the map, and have a label for that marker.

I have figured out how to hide the label however, I am at a bit of a loss on how to hide the icon and show the label. Anyone with some suggestions on this?

markerWithLabel.setMap(null);

removes the marker and label, not just the marker.

Share Improve this question edited Oct 16, 2016 at 2:44 Ravi Ram 24.5k21 gold badges86 silver badges119 bronze badges asked Oct 2, 2013 at 17:36 BigDubbBigDubb 4787 silver badges19 bronze badges 2
  • can you share what you have done so far ?? what do you mean by label.. – Vishal Sharma Commented Oct 2, 2013 at 17:40
  • If you want to control them separately, you might need to modify that code. Or put the labels on the map independently of the marker (i.e. use InfoBox for the label and a normal google.maps.Marker for the marker) – geocodezip Commented Oct 2, 2013 at 17:46
Add a comment  | 

2 Answers 2

Reset to default 19

I was able to hide the marker by setting the icon setting to an empty string " ".

var marker = new MarkerWithLabel({
            icon: " ",
            position: location,
            draggable: true,
            raiseOnDrag: true,
            map: map,
            labelContent: z, 
            labelAnchor: new google.maps.Point(22, 0),
            labelClass: "labels", // the CSS class for the label
            labelStyle: {opacity: 0.75},
        });

Very easy, just set a transparent image as the icon. Here I set a 1x1 transparent GIF - pixel_trans.gif - as Icon when the MarkerWithLabel is clicked :

var marker = new MarkerWithLabel({
...
});

google.maps.event.addListener(marker, 'click', function() {
  this.setIcon('pixel_trans.gif');
});

Viola, the marker is hidden when it is clicked, but the label stand. It is not possible to hide the Icon and only show the label in the current version of MarkerWithLabel.


Note: You have to pass a valid image before this is going to work, eg an image that exists. Using setIcon with null or an invalid image will be ignored. An 1x1 transparent gif is imho an acceptable solution, since the user will have the experience that the marker is hidden, and will have a h*** of a trouble trying to click on it :)

发布评论

评论列表(0)

  1. 暂无评论