RositsaV
RositsaV

Reputation: 101

Customising MapInfoWindow (xamarin.forms.maps) and add second label if it's possible using android - xamarin

I want to remove this elements from info window

I'm new to using xamarin.forms.maps and want to remove the Logo, this monkey from the left side and this icon(i) from the right side.

Also I want to add a second label if it's possible ?

With this code I call the pin and set the coordinates and other stuff:

CustomPin pin = new CustomPin
        {
            Type = PinType.Place,
            Position = new Position(41.56488683065354, 26.13941661843389),
            Label = "р. Чорбаджийска",
            Name = "Xamarin",
            Address = "с. Островец",
        };

I have 17 pins and want to add a variable on the second Label under Address.

What can I do add second label.

My objects from the Pin are:

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

I want to set AlertLevel like a second label under the address.

How can I remove this three images on the info window ?

My CustomRenderer look like this:

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

Upvotes: 0

Views: 373

Answers (1)

Leo Zhu
Leo Zhu

Reputation: 14956

You could see the method GetInfoContents

if (customPin.Name.Equals("Xamarin"))
     {
           view = inflater.Inflate(Resource.Layout.XamarinMapInfoWindow, null);
     }
else
     {
           view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);
     }

this is where you inflate the ui with Resource.Layout.XamarinMapInfoWindow or Resource.Layout.MapInfoWindow.

So you just need open the XamarinMapInfoWindow.xml and MapInfoWindow.xml inside Resources/layout folder ,delete the corresponding ImageView or ImageButton and you also could add a second label inside it.

Upvotes: 1

Related Questions