简体   繁体   English

在Google Maps JS API中为标记添加数字

[英]Adding a number to a marker in Google Maps JS API

I currently have markers displaying as below, but without the numbers that signify there is more than one item in that exact location. 目前,我的标记显示如下,但没有数字表示该确切位置中有多个项目。

I would like the markers to display as shown below: 我希望标记显示如下:

在此处输入图片说明

I was able to in back-end group the items based on exact location and that is fine, but I can't seem to find a way to add the special number/count to show how many items are there without creating special images with numbers drawn in them. 我能够根据确切的位置在后端分组项目,这很好,但是我似乎找不到一种方法来添加特殊编号/计数以显示其中有多少项目,而不用创建带有编号的特殊图像画在他们身上。 Ideas on how this could be done? 关于如何做到这一点的想法?

One other direction if I can't doing it cleanly is use PIL to add the number to the image. 如果我不能干净地做的另一个方向是使用PIL将数字添加到图像中。 Still I don't think that is ideal. 我仍然认为那不是理想的。

So, what you want to do is inject HTML instead of images in the custom markers. 因此,您要做的是在自定义标记中注入HTML而不是图像。 It's essentially what you are doing but google allows you to OverlayViews , even of HTML type. 从本质OverlayViews ,这是您在做什么,但是google允许您OverlayViews ,甚至是HTML类型的。 See fiddle 见小提琴

HTMLMarker.prototype.onAdd= function(){
    div = document.createElement('DIV');
    div.className = "htmlMarker";
    div.innerHTML = '<img src="http://www.w3schools.com/html/pic_mountain.jpg" alt="Mountain View" style="width:30px;height:22px">'+ 'your number count'; //This HTML here is driven by your login to have numbers.
    var panes = this.getPanes();
    panes.overlayImage.appendChild(div);
}

===[ UPDATE ]=== === [ 更新 ] ===

So, I tried checking if I could add multiple overlays on the map . 因此, 我尝试检查是否可以在地图上添加多个叠加层 I could but they somehow ended up at the same location, and I tried multiple iterations but was not very successful in separating them out. 我可以,但是它们以某种方式最终出现在同一位置,并且我尝试了多次迭代,但是在将它们分离出来方面并不是很成功。

So, since you are essentially looking for numbering on markers, I looked around for other ways to solve this issue. 因此,由于您本质上是在寻找标记的编号方式,因此我四处寻找解决该问题的其他方法。 I found this library on google maps, markers with labels . 我在Google地图上找到了这个带有标签的标记库。

You can find a fiddle on how I customized the markers here . 您可以在这里找到有关如何自定义标记的小提琴。

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

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