【问题标题】:Xamarin GoogleMaps - Acceptable pin/marker colorsXamarin GoogleMaps - 可接受的图钉/标记颜色
【发布时间】:2018-05-10 23:15:31
【问题描述】:

我在我的便携式项目中使用 Xamarin.Forms.GoogleMaps。 它在 iOS 和 Android 上运行良好。 现在我正在研究 googleMap 的图钉/标记。 我正在尝试添加不同颜色的图钉,但并非所有颜色都有效。

我现在的表现如何:

    var pin = new Xamarin.Forms.GoogleMaps.Pin();
    pin.Label = ... etc etc etc
    pin.Icon = BitmapDescriptorFactory.DefaultMarker(Color.Azure);

上面的例子效果很好。但是,例如,如果我将其更改为Color.Black,它将不起作用并以默认的红色显示标记。而且它在不同的平台上也有问题,在 iOS 中,Black 工作,在 Android 中没有。 (没有出现错误,只显示默认红色而不是黑色)

所以我的问题是:
是否有可接受的颜色列表用作标记/针颜色?每种颜色都应该有效还是只使用一些预定义的颜色?

另外,我如何将默认图标更改为另一个图标图像?我尝试使用“FromBundle”,但至少对我来说会引发错误。(也许我做错了。它告诉图像需要是位图)

如果可能的话,我想避免使用自定义渲染器,因为现在它在没有任何自定义渲染的情况下工作得很好(除了我说的一些颜色)。

【问题讨论】:

标签: c# google-maps xamarin google-maps-api-3 xamarin.forms


【解决方案1】:

如果您不想使用自定义渲染器,Xamarin Forms Map 标记/图钉只有 300 多种颜色可供使用。

但是,如果您想按照自己的意愿表示标记/引脚颜色,则需要实现 Xamarin Forms Custom Renderers 以实现/捕获您想要的确切标记/引脚颜色。

按照 Xamarin Forms 自定义渲染器文档中的步骤操作后,覆盖以下方法:

    protected override MarkerOptions CreateMarker(Pin pin)
    {
        CustomPin customPin = (CustomPin)pin;
        var marker = new MarkerOptions();
        marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
        marker.SetTitle(pin.Label);
        marker.SetSnippet(pin.Address);
        marker.SetIcon(GetCustomBitmapDescriptor(customPin.Color));
        return marker;
    }

然后我创建了以下方法,它将对标记/引脚颜色进行实际更改,这将是您的 RGB/Hex 颜色代码中的确切颜色:

    private BitmapDescriptor GetCustomBitmapDescriptor(string text)
    {
        using (Paint paint = new Paint(PaintFlags.AntiAlias))
        {
            using (Rect bounds = new Rect())
            {
                using (Bitmap baseBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.marker))
                {

                    Bitmap resultBitmap = Bitmap.CreateBitmap(baseBitmap, 0, 0, baseBitmap.Width - 1, baseBitmap.Height - 1);
                    Paint p = new Paint();
                    ColorFilter filter = new PorterDuffColorFilter(Android.Graphics.Color.ParseColor(text), PorterDuff.Mode.SrcAtop);
                    p.SetColorFilter(filter);
                    Canvas canvas = new Canvas(resultBitmap);
                    canvas.DrawBitmap(resultBitmap, 0, 0, p);
                    Bitmap scaledImage = Bitmap.CreateScaledBitmap(resultBitmap, 94, 150, false);

                    BitmapDescriptor icon = BitmapDescriptorFactory.FromBitmap(scaledImage);
                    resultBitmap.Recycle(); 
                    return (icon);
                }
            }
        }
    }

注意事项:

  • 此示例代码仅适用于 Android。不确定是否适用于 iOS。

  • Resource.Drawable.marker 可以是您可以使用的任何标记。我刚刚在网上下载了一个通用的红色地图标记。无论如何,它都会被 GetCustomBitmapDescriptor 方法覆盖。

  • resultBitmap.Recycle();非常重要。因为位图占用大量内存并且设备应用程序可能会停止,所以您需要重用位图内存。

  • CustomPin 是扩展 Xamarin.Forms.Map.Pin 类的类。我为我想要的标记针颜色的字符串十六进制值添加了一个字符串属性。

查看带有自定义颜色的地图示例图像。

【讨论】:

  • 我用的是Xamarin Forms Maps,但是用的是各个平台的原生地图,iOS地图不好,所以决定换成Xamarin Google Maps(不支持覆盖 MarkerOptions),它在两者中都很好用,但有些颜色没有显示出来,只是其中一些,我认为 Christopher Stephan 的评论是正确的。但无论如何,Tahnks
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 2014-03-12
  • 1970-01-01
  • 2011-04-26
  • 2014-12-24
  • 2017-07-28
相关资源
最近更新 更多