Я реализовал кластеры карт Google для своего Xamarin.iOS.
У меня есть собственный кластер с количеством маркеров в нем.
Вот
![example](https://i.stack.imgur.com/5DGOu.png)
Я создаю собственный IconGenerator с унаследованным от GMUDefaultClusterIconGenerator
class CustomIconGenerator : GMUDefaultClusterIconGenerator
{
public override UIImage IconForSize(nuint size)
{
UIImage textToImage = TextToImage(size);
return textToImage;
}
private UIImage TextToImage(nuint size)
{
var text = size.ToString();
var uiImage = UIImage.FromBundle("markClaster");
nfloat fWidth = uiImage.Size.Width;
nfloat fHeight = uiImage.Size.Height;
CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
using (CGBitmapContext ctx = new CGBitmapContext(IntPtr.Zero, (nint) fWidth, (nint) fHeight, 8
, 4 * (nint) fWidth, CGColorSpace.CreateDeviceRGB()
, CGImageAlphaInfo.PremultipliedFirst))
{
ctx.DrawImage(new CGRect(0, 0, (double) fWidth, (double) fHeight), uiImage.CGImage);
ctx.SelectFont("Roboto-Bold", 12, CGTextEncoding.MacRoman);
//Measure the text's width - This involves drawing an invisible string to calculate the X position difference
float start, end, textWidth;
//Get the texts current position
start = (float) ctx.TextPosition.X;
//Set the drawing mode to invisible
ctx.SetTextDrawingMode(CGTextDrawingMode.Invisible);
//Draw the text at the current position
ctx.ShowText(text);
//Get the end position
end = (float) ctx.TextPosition.X;
//Subtract start from end to get the text's width
textWidth = end - start;
//Set the fill color to black. This is the text color.
ctx.SetFillColor(UIColor.White.CGColor);
//Set the drawing mode back to something that will actually draw Fill for example
ctx.SetTextDrawingMode(CGTextDrawingMode.Fill);
//Draw the text at given coords.
ctx.ShowTextAtPoint(uiImage.Size.Width / 2 - textWidth / 2, uiImage.Size.Height / 2, text);
return UIImage.FromImage(ctx.ToImage());
}
}
}
но результат не впечатляет
, если я изменю FontName
на Helvetica
, результат получится немного лучше, но не идеально.
Шрифт размыт и не центрирован.
![enter image description here](https://i.stack.imgur.com/jX0uz.jpg)
Я думал, что, может быть, решение для установки xib или UIView в качестве кластера существует, но я не нашел.