问题描述
我正在尝试将传递到 InfoWindow 中的自定义图钉的对象图像添加到 InfoWindow 的左侧。
我有一个应用程序调用 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" />
解决方法
创建具有 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);
});
});