我想在地图上添加标签。我在地图上有一堆多边形绘制果园(使用Xamarin表单和Android和iOS自定义渲染(,并想在每个标签上添加一些详细信息。我尝试过下面的方法,用文本作为标记制作一个自定义pin,但这并不理想,因为我希望每个pin中有几行文本,不需要它可以点击。我还需要这个在iOS和Android中工作。
如有任何帮助,我们将不胜感激。
public Marker AddText(Context context, GoogleMap map, LatLng location, string text, int fontSize, Polygon polygon)
{
try
{
if (text == null)
throw new ArgumentNullException(nameof(text));
if (location == null)
throw new ArgumentNullException(nameof(location));
if (map == null)
throw new ArgumentNullException(nameof(map));
if (context == null)
throw new ArgumentNullException(nameof(context));
if (fontSize <= 0)
throw new ArgumentOutOfRangeException(nameof(fontSize));
var textView = new TextView(context);
textView.Text = text;
textView.TextSize = fontSize;
var paintText = textView.Paint;
var boundsText = new Rect();
paintText.GetTextBounds(text, 0, textView.Length(), boundsText);
paintText.TextAlign = Paint.Align.Center;
paintText.Color = Android.Graphics.Color.White;
paintText.FakeBoldText = true;
var bmpText = Bitmap.CreateBitmap(boundsText.Width() + 10, boundsText.Height(), Bitmap.Config.Argb8888);
var canvasText = new Canvas(bmpText);
canvasText.DrawText(text, canvasText.Width / 2, canvasText.Height - boundsText.Bottom, paintText);
var markerOptions = new MarkerOptions();
markerOptions.SetPosition(location);
markerOptions.SetIcon(BitmapDescriptorFactory.FromBitmap(bmpText));
markerOptions.Anchor(0.5f, 0.5f);
//markerOptions.SetTitle(text);
Marker marker = map.AddMarker(markerOptions);
Constants.markerPolygons.Add(markerOptions.Position.ToString(), polygon);
lstText.Add(marker);
return marker;
}
我想你要找的是SkiaSharp。https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/
我还建议您考虑Xamarin。表格。多个多边形的GoogleMapshttps://github.com/amay077/Xamarin.Forms.GoogleMaps