1

我正在尝试将对象的图像添加到信息窗口左侧的信息窗口中的自定义引脚。

我有一个调用 API 并返回具有名称和图像的对象的应用程序,我希望 InfoWindow 的左侧图标成为对象的图像,但我不知道该怎么做。

在此处输入图像描述

左图。

谢谢您的帮助。

对于科尔

自定义地图渲染器

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");
            }

            view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);


            var infoTitle = view.FindViewById<TextView>(Resource.Id.InfoWindowTitle);
            var infoSubtitle1 = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle1);
            ImageView image = view.FindViewById<ImageView>(Resource.Id.image);

            Task.Run(() => {
                URL url = new URL(customPin.Image);
                var mIcon_val = BitmapFactory.DecodeStream(url.OpenConnection().InputStream);
                image.SetImageBitmap(mIcon_val);
            });

            if (infoTitle != null)
            {
                infoTitle.Text = marker.Title;
            }
            if (infoSubtitle1 != null)
            {
                infoSubtitle1.Text = marker.Snippet;
            }

            return view;
        }
        return null;
    }

地图信息窗口

<?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="horizontal">
<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="10dp">
    <TextView
        android:id="@+id/InfoWindowTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="InfoWindowTitle"
        android:textColor="@android:color/black"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/InfoWindowSubtitle1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="InfoWindowSubtitle1"
        android:textColor="@android:color/black" />
</LinearLayout>
<ImageButton
    android:id="@+id/InfoWindowButton"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/mtrl_btn_transparent_bg_color"
    android:src="@drawable/icon_heart_red_24" />
4

1 回答 1

0

创建具有Name Image属性的自定义类。

public class CustomPin : Pin
{
    public string Name { get; set; }
    public string Image{ get; set; }
}

使用 API 返回的数据填充自定义 pin,并将 pin 添加到map.Pins.

 CustomPin pin = new CustomPin
    {
        Type = PinType.Place,
        Position = new Position(37.79752, -122.40183),
        Label = "Xamarin San Francisco Office",
        Address = "394 Pacific Ave, San Francisco CA",       
        Url = "http://xamarin.com/about/",
        Name = "xxxxx",
        Image =  "xxxx";
    };
    customMap.CustomPins = new List<CustomPin> { pin };
    customMap.Pins.Add(pin);

覆盖 CreateMarker 方法

protected override MarkerOptions CreateMarker(Pin pin)
{
    var marker = new MarkerOptions();

    CustomPin pin2 = pin;

    marker.SetPosition(new LatLng(pin2.Position.Latitude, pin2.Position.Longitude));
    marker.SetTitle(pin2.Label);
    marker.SetSnippet(pin2.Address);

    URL url = new URL(pin2.Image);
    Bitmap bmp = BitmapFactory.DecodeStream(url.OpenConnection().InputStream);
    marker.SetIcon(BitmapDescriptorFactory.FromBitmap(bmp));
    return marker;
}

更新

ImageView image  view.FindViewById<ImageView>(Resource.Id.image);

Task.Run(() => {
     URL url = new URL(customPin.Image);
     var mIcon_val = BitmapFactory.DecodeStream(url.OpenConnection().InputStream);

     MainThread.BeginInvokeOnMainThread(()=> {
        image.SetImageBitmap(mIcon_val);
    });
});
于 2021-06-17T04:34:10.690 回答