c# - 自定义 MapInfoWindow (xamarin.forms.maps) 并添加第二个标签,如果可以使用 android - xamarin
问题描述
我是新手xamarin.forms.maps
,想从左侧删除徽标,从左侧删除此猴子,从右侧删除此图标(i)。
如果可能的话,我还想添加第二个标签吗?
使用此代码,我调用 pin 并设置坐标和其他内容:
CustomPin pin = new CustomPin
{
Type = PinType.Place,
Position = new Position(41.56488683065354, 26.13941661843389),
Label = "р. Чорбаджийска",
Name = "Xamarin",
Address = "с. Островец",
};
我有 17 个引脚,想在Address
.
我能做些什么添加第二个标签。
我来自 Pin 的对象是:
using Xamarin.Forms.Maps;
namespace CustomRenderer
{
public class CustomPin : Pin
{
public string Name { get; set; }
public string Url { get; set; }
public int CodeNum { get; set; }
public int AlertLevel { get; set; }
}
}
我想AlertLevel
在address
.
如何删除信息窗口上的这三个图像?
我的 CustomRenderer 看起来像这样:
using System;
using System.Collections.Generic;
using Android.Content;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using Android.Widget;
using CustomRenderer;
using CustomRenderer.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Xamarin.Forms.Maps.Android;
[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace CustomRenderer.Droid
{
public class CustomMapRenderer : MapRenderer, GoogleMap.IInfoWindowAdapter
{
List<CustomPin> customPins;
public CustomMapRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
NativeMap.InfoWindowClick -= OnInfoWindowClick;
}
if (e.NewElement != null)
{
var formsMap = (CustomMap)e.NewElement;
customPins = formsMap.CustomPins;
}
}
protected override void OnMapReady(GoogleMap map)
{
base.OnMapReady(map);
NativeMap.InfoWindowClick += OnInfoWindowClick;
NativeMap.SetInfoWindowAdapter(this);
}
protected override MarkerOptions CreateMarker(Pin 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(BitmapDescriptorFactory.FromResource(Resource.Drawable.green));
return marker;
}
void OnInfoWindowClick(object sender, GoogleMap.InfoWindowClickEventArgs e)
{
var customPin = GetCustomPin(e.Marker);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
if (!string.IsNullOrWhiteSpace(customPin.Url))
{
var url = Android.Net.Uri.Parse(customPin.Url);
var intent = new Intent(Intent.ActionView, url);
intent.AddFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
}
}
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);
if (infoTitle != null)
{
infoTitle.Text = marker.Title;
}
if (infoSubtitle != null)
{
infoSubtitle.Text = marker.Snippet;
}
return view;
}
return null;
}
public Android.Views.View GetInfoWindow(Marker marker)
{
return null;
}
CustomPin GetCustomPin(Marker annotation)
{
var position = new Position(annotation.Position.Latitude, annotation.Position.Longitude);
foreach (var pin in customPins)
{
if (pin.Position == position)
{
return pin;
}
}
return null;
}
}
}
解决方案
你可以看到方法GetInfoContents
if (customPin.Name.Equals("Xamarin"))
{
view = inflater.Inflate(Resource.Layout.XamarinMapInfoWindow, null);
}
else
{
view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);
}
这是您使用Resource.Layout.XamarinMapInfoWindow
或为 ui 充气的地方Resource.Layout.MapInfoWindow
。
所以你只需要打开XamarinMapInfoWindow.xml
andMapInfoWindow.xml
里面的Resources/layout
文件夹,删除对应的ImageView
or ImageButton
,你也可以在里面添加第二个标签。
推荐阅读
- pyspark - Pyspark:不是分区上的累积总和
- c++ - 之后如何对 RGB 向量进行排序以对其进行二进制搜索?
- ios - iOS | 斯威夫特 | 日期格式 | 出了什么问题?
- reactjs - 在 React 中使用 useState 钩子为多个集合状态渲染一次
- python - 在 python 中使用 XML 模式解析未定界的文本文件
- node.js - 猫鼬没有找到价值
- r - 对每行不同范围的列求和
- reactjs - 在 reactjs 前端看不到来自 django 的数据
- jakarta-ee - 如何找出 WildFly 使用了哪些 Jakarta EE 实现?
- java - 如何监听 JTable 行和列的变化