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

时间:2019-03-25 15:28:52

标签: google-maps xamarin.forms google-maps-markers xamarin.forms.maps

我想在Xamarin表单地图中创建一个自定义信息窗口,我该如何实现。我正在使用xamarin.forms.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;
}
    }
}

1 个答案:

答案 0 :(得分:1)

  

您可以定义一个基于Xamarin.Forms组件的组件,然后   可以像添加一些可绑定属性,对象等一样对其进行修改。您也可以从诸如stack,flow等的布局派生而不是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
    {
    }
}