You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin.Forms.Maps安卓端自定义MapInfoWindow 删除图标并新增第二标签

实现步骤

1. 调整安卓端InfoWindow布局文件

首先修改安卓项目Resources/layout目录下的两个布局文件XamarinMapInfoWindow.axml和MapInfoWindow.axml,移除原有布局中所有的ImageView控件(对应猴子图标、(i)图标、窗口Logo),新增AlertLevel对应的TextView,示例布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp"
    android:background="#ffffff">
    <TextView
        android:id="@+id/InfoWindowTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="16sp"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/InfoWindowSubtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#666666"
        android:textSize="14sp"
        android:layout_marginTop="4dp"/>
    <!-- 新增AlertLevel展示控件 -->
    <TextView
        android:id="@+id/InfoWindowAlertLevel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ff0000"
        android:textSize="14sp"
        android:layout_marginTop="4dp"/>
</LinearLayout>

两个布局保持一致结构即可,如果需要不同样式可以单独调整。

2. 补充CustomPin实例的AlertLevel赋值

你现有的17个CustomPin实例化时都要补上AlertLevel属性的赋值,示例:

CustomPin pin = new CustomPin
{
    Type = PinType.Place,
    Position = new Position(41.56488683065354, 26.13941661843389),
    Label = "р. Чорбаджийска",
    Name = "Xamarin",
    Address = "с. Островец",
    AlertLevel = 2 // 补充AlertLevel赋值,根据你的业务需求填对应值
};

所有17个标记都按上述格式补充即可。

3. 修改安卓自定义渲染器的GetInfoContents方法

调整渲染器中InfoWindow的赋值逻辑,新增AlertLevel的绑定:

public Android.Views.View GetInfoContents(Marker marker)
{
    var inflater = Android.App.Application.Context.GetSystemService(Context.LayoutInflaterService) as Android.Views.LayoutInflater;
    if (inflater != null)
    {
        Android.Views.View view;

        var customPin = GetCustomPin(marker);
        if (customPin == null)
        {
            throw new Exception("Custom pin not found");
        }

        if (customPin.Name.Equals("Xamarin"))
        {
            view = inflater.Inflate(Resource.Layout.XamarinMapInfoWindow, null);
        }
        else
        {
            view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);
        }

        var infoTitle = view.FindViewById<TextView>(Resource.Id.InfoWindowTitle);
        var infoSubtitle = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle);
        // 新增AlertLevel控件实例获取
        var infoAlertLevel = view.FindViewById<TextView>(Resource.Id.InfoWindowAlertLevel);

        if (infoTitle != null)
        {
            infoTitle.Text = marker.Title;
        }
        if (infoSubtitle != null)
        {
            infoSubtitle.Text = marker.Snippet;
        }
        // 新增AlertLevel赋值,可根据需求拼接展示文本
        if (infoAlertLevel != null)
        {
            infoAlertLevel.Text = $"告警等级:{customPin.AlertLevel}";
        }

        return view;
    }
    return null;
}

说明

  • 布局中已经移除了所有多余的图标控件,所以原有猴子图标、(i)图标、窗口Logo都会自动消失,不需要额外处理
  • 该适配逻辑对所有CustomPin类型的标记生效,你17个标记只要都正确赋值AlertLevel,就会自动按规则展示,不需要单独适配每个标记

内容的提问来源于stack exchange,提问作者RositsaV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 07:45:06