首页 > 解决方案 > 自定义 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; }
    }
}

我想AlertLeveladdress.

如何删除信息窗口上的这三个图像?

我的 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;
        }
    }
}

标签: c#androidxamarinxamarin.forms

解决方案


你可以看到方法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.xmlandMapInfoWindow.xml里面的Resources/layout文件夹,删除对应的ImageViewor ImageButton,你也可以在里面添加第二个标签。


推荐阅读