【问题标题】:How to relocate the position of the myLocationButton and true North button in Xamarin forms?如何在 Xamarin 表单中重新定位 myLocationButton 和真北按钮的位置?
【发布时间】:2018-03-18 23:00:05
【问题描述】:

我正在使用带有自定义地图的 Xamarin.Forms.Maps 和带有 Droid Renderer 的自定义 pin。我想重新定位地图右下角的 myLocationButton 和地图左下角的北按钮。

这是自定义渲染器。 [组装:ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]

命名空间 CustomRenderer.Droid{

public class CustomMapRenderer:MapRenderer,GoogleMap.IInfoWindowAdapter
{
    List<CustomPin> customPins;
    bool isDrawn;

    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 OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName.Equals("VisibleRegion") && !isDrawn)
        {
            NativeMap.Clear();
            NativeMap.UiSettings.ScrollGesturesEnabled = true;
            NativeMap.InfoWindowClick += OnInfoWindowClick;
            NativeMap.SetInfoWindowAdapter(this);
            NativeMap.UiSettings.MyLocationButtonEnabled = true;
            NativeMap.MyLocationEnabled = true;

            foreach (var pin in customPins)
            {
                var marker = new MarkerOptions();
                marker.SetPosition(new LatLng(pin.Pin.Position.Latitude, pin.Pin.Position.Longitude));
                marker.SetTitle(pin.Pin.Label);
                marker.SetSnippet(pin.Pin.Address);
                if (pin.Id == "0")
                {
                    marker.SetIcon(BitmapDescriptorFactory.FromResource(project.Droid.Resource.Drawable.googlecab));
                }
                else if (pin.Id == "1")
                {
                    marker.SetIcon(BitmapDescriptorFactory.FromResource(project.Droid.Resource.Drawable.googlehome));
                }
                else if (pin.Id == "2")
                {
                    marker.SetIcon(BitmapDescriptorFactory.FromResource(project.Droid.Resource.Drawable.googlerv));
                }
                else if (pin.Id == "Xamarin")
                {
                    marker.SetIcon(BitmapDescriptorFactory.FromResource(project.Droid.Resource.Drawable.googlerv));

                }

                NativeMap.AddMarker(marker);
            }
            isDrawn = false;
        }
    }

    protected override void OnLayout(bool changed, int l, int t, int r, int b)
    {
        base.OnLayout(changed, l, t, r, b);

        if (changed)
        {
            isDrawn = false;
        }
    }

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

            MessagingCenter.Send(new CategoryItems()
            {
                Vid = customPin.vId,
                VType = customPin.vType
            }, "UserName");
        }
    }



    public Android.Views.View GetInfoContents(Marker marker)
    {
        isDrawn = true;
        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");
            }

            view = inflater.Inflate(project.Droid.Resource.Layout.XamarinMapInfoWindow, null);

            var infoTitle = view.FindViewById<TextView>(project.Droid.Resource.Id.InfoWindowTitle);
            var infoSubtitle = view.FindViewById<TextView>(project.Droid.Resource.Id.InfoWindowSubtitle);
            var infoBg = view.FindViewById<ImageView>(project.Droid.Resource.Id.InfoWindowBackgroundImage);

            if (infoTitle != null)
            {
                infoTitle.Text = marker.Title;
            }
            if (infoSubtitle != null)
            {
                infoSubtitle.Text = marker.Snippet;
            }
            if (infoBg != null){
                if(!string.IsNullOrEmpty(customPin.Url)){
                    Picasso.With(Context)
                       .Load(customPin.Url)
                       .Into(infoBg);
                }
            }

            return view;
        }
        isDrawn = true;
        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.Pin.Position == position)
            {
                return pin;
            }
        }
        return null;
    }
}

}

【问题讨论】:

    标签: c# xamarin.forms xamarin.android


    【解决方案1】:

    我使用了自定义渲染器,因此我试图通过使用 FindViewByID 的 ID 来获取 myLocationButton,但没有成功。

    我终于找到了解决方案。不要获取视图并设置其布局。你只需要给地图一个填充。 就这些了

    NativeMap.SetPadding(0,800,0,0);

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      相关资源
      最近更新 更多