xamarin - 当注释的位置在自定义地图 Xamarin.Android 上可见时调用方法
问题描述
这篇文章是这个主题的延续, 我需要为 Android 的 CustomMapRenderer 找到等效的方法 Xamarin.Forms.Maps.iOS.MapRenderer.GetViewForAnnotation。对于本文档,当注解的位置在地图上可见时调用 iOS 的 GetViewForAnnotation 方法,并用于在显示之前自定义注解。我需要找到等效项,因为我想在地图上显示带有已展开窗口消息的图钉(无需单击它们)。
解决方案
从自定义引脚获取标记后,您可以直接显示信息窗口。
重写 CreateMarker 方法时添加以下代码。
NativeMap.AddMarker(marker).ShowInfoWindow();
Android的全自定义渲染器:
public class CustomMapRenderer : MapRenderer, GoogleMap.IInfoWindowAdapter
{
List<CustomPin> customPins;
public CustomMapRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(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.pin));
NativeMap.AddMarker(marker).ShowInfoWindow();
return marker;
}
private 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);
}
}
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;
}
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;
}
}
有关更多详细信息,您可以参考 MS 文档。 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/map-pin
推荐阅读
- excel - 如何标记两组不同的重复值?
- visual-studio - Nuget 依赖解决方案
- excel - 使用excel VBA的复选框上的可变标题
- openmodelica - fmi - 忽略未知资源 URI
- php - 大型序列化 PHP 数组文件
- 2sxc - 如何在 2sxc 的模块之间移动内容项?
- mongodb - 使用 $in 或 $gt, $lt ($gte, $lte) 哪个更好?
- php - 在 Mediawiki 中上传时调整图像大小
- amazon-web-services - 我无法让我的自动缩放组引用我的 TargetGroup
- react-native - 移动到屏幕而不将其添加到 createTabNavigator