简体   繁体   English

当注释的位置在自定义上可见时调用方法 map Xamarin.Android

[英]Call method when the location of the annotation becomes visible on the custom map Xamarin.Android

This post is continuation of this topic I need find equivalent method Xamarin.Forms.Maps.iOS.MapRenderer.GetViewForAnnotation for CustomMapRenderer for Android. As for this documentation the GetViewForAnnotation method for iOS is called when the location of the annotation becomes visible on the map, and is used to customize the annotation prior to display. This post is continuation of this topic I need find equivalent method Xamarin.Forms.Maps.iOS.MapRenderer.GetViewForAnnotation for CustomMapRenderer for Android. As for this documentation the GetViewForAnnotation method for iOS is called when the location of the annotation becomes visible on the map, and用于在显示之前自定义注释。 I need to find equivalent because I want to show pins on my map with already expanded window message(without click on them).我需要找到等效项,因为我想在我的 map 上显示带有已展开的 window 消息的引脚(无需单击它们)。

You could show the InfoWindow directly after you get the marker from your custom pin.从自定义引脚获取标记后,您可以直接显示信息窗口。

Add the code below when you override the CreateMarker method.覆盖 CreateMarker 方法时添加以下代码。

NativeMap.AddMarker(marker).ShowInfoWindow();

The whold custom renderer of Android: 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;
    }
}

For more details, you could refer to the MS docs.有关更多详细信息,您可以参考 MS 文档。 https://learn.microsoft.com/en-us/xamarin/xamarin.forms/app-fundamentals/custom-renderer/map-pin https://learn.microsoft.com/en-us/xamarin/xamarin.forms/app-fundamentals/custom-renderer/map-pin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM