如何在xamarin地图中制作自定义信息窗口?

问题描述 投票:0回答:1

我想在我的Xamarin表单映射中创建一个自定义信息窗口,我该如何实现它。我正在使用xamarin.forms.map map plugin来创建map。请帮帮我,我想要一个像this这样的自定义信息窗口

我制作自定义地图和自定义图钉

 public class CustomMap : Map
    {
        public List<CustomPin> CustomPins { get; set; }
        }

定制别针

public class CustomPin : Pin
    {
        public string ImageUrl { get; set; }

        public float rating { get; set; }
}

地图页面xaml.cs

public partial class MapPage : ContentPage
    {
        CustomPin pin;
        MapVM MapVM;
        public MapPage()
        {
            InitializeComponent();

            pin = new CustomPin
            {
                Type = PinType.Place,
                Position = new Position(37.79752, -122.40183),
                Label = "Xamarin San Francisco Office",
                Address = "394 Pacific Ave, San Francisco CA",
                Url = "http://xamarin.com/about/",
                rating = 3

            };
            var pin1 = new CustomPin
            {
                Type = PinType.Place,
                Position = new Position(38.79752, -124.40183),
                Label = "Xamarin San Francisco Office",
                Address = "395 Pacific Ave, San Francisco CA",
                Url = "http://xamarin.com/about/",
                rating=2

            };

            customMap.CustomPins = new List<CustomPin> { pin, pin1 };
             customMap.Pins.Add(pin);
            customMap.Pins.Add(pin1);
            customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(37.79752, -122.40183), Distance.FromMiles(1.0)));
        }
}

现在我不知道自定义渲染类。请帮助我如何定义自定义渲染类以及如何指定图像值和评级值以显示在信息窗口中。

CustomRender.cs

[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;
                Control.GetMapAsync(this);
            }
        }

        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);
    return marker;
}
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.Id.ToString() == "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);
 var imageView = view.FindViewById<ImageView>(Resource.Id.image);
            var ratings = view.FindViewById<RatingBar>(Resource.Id.ratingbar);
//here how i set values for Image and Rating Cotrol

    if (infoTitle != null) {
      infoTitle.Text = marker.Title;
    }
    if (infoSubtitle != null) {
      infoSubtitle.Text = marker.Snippet;
    }

    return view;
  }
  return null;
}
    }
}
google-maps xamarin.forms google-maps-markers xamarin.forms.maps
1个回答
1
投票

您可以定义一个基于Xamarin.Forms组件的组件,您可以像添加一些可绑定属性,对象等一样修改它。此外,您可以从堆栈,流等等布局派生而不是Xamarin.Forms组件。因此,您可以提高自定义级别。

在Android方面

    [assembly: ExportRenderer(typeof(CustomEntry), typeof(AndroidCustomEntryRenderer))]
namespace MyProject.Droid.Renderer
{
    public class AndroidCustomEntryRenderer : EntryRenderer
    {
        public AndroidCustomEntryRenderer(Context context) : base(context)
        {
        }
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.SetBackgroundColor(global::Android.Graphics.Color.White);

            }
        }
    }
} 

在PCL方面

namespace MyProject.Views.ViewComponents
{
    public class CustomEntry : Entry
    {
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.