【问题标题】:Adding Labels to Xamarin map将标签添加到 Xamarin 地图
【发布时间】:2020-02-18 19:06:27
【问题描述】:

我想为我的地图添加标签。 我在映射出果园的地图上有一堆多边形(使用带有 Android 和 iOS 自定义渲染的 Xamarin 表单),并希望为每个多边形添加一些带有详细信息的标签。我已经尝试了以下方法,您可以使用用作标记的文本制作自定义图钉,但这并不理想,因为我希望每行文本中有几行并且不需要它可点击。我还需要它才能在 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;
        }

【问题讨论】:

  • 您在哪里调用 AddText?您能否添加更多关于您的自定义渲染器的代码?也请与我们分享您想要实现的更好的图片。

标签: xamarin.forms xamarin.android xamarin.ios android-maps-v2


【解决方案1】:

我认为您正在寻找的是 SkiaSharp。 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/

我还建议您考虑将 Xamarin.Forms.GoogleMaps 用于多个多边形 https://github.com/amay077/Xamarin.Forms.GoogleMaps

【讨论】:

  • 工作完美,谢谢。还全部切换到 Forms.Googlemaps 而不是使用自定义渲染,哇,这太容易了!
猜你喜欢
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 2018-10-21
  • 2011-04-03
  • 2016-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多