【问题标题】:Xamarin.Forms.Maps pins with expanded message window带有扩展消息窗口的 Xamarin.Forms.Maps 引脚
【发布时间】:2021-09-20 09:02:52
【问题描述】:

我正在使用 Xamarin.Forms.Map,我想在我的地图上显示带有已展开窗口消息的图钉(无需单击它们)。截图之类的。默认情况下,只有在我点击它们之后才会显示窗口消息。我该怎么做?

【问题讨论】:

    标签: xamarin.forms xamarin.android xamarin.ios xamarin.forms.maps


    【解决方案1】:

    我根据这个样本做了一个测试:sample

    我所做的是重写 GetViewForAnnotation 方法。 我添加了一个子视图并根据引脚的位置设置它的位置。

    这里是相关代码:

    [assembly:ExportRenderer(typeof(CustomMap),typeof(CustomMapRenderer))]
    namespace My_Forms_Test3.iOS
    {
        public class CustomMapRenderer:MapRenderer
        {
            UIView customPinView;
            List<CustomPin> customPins;
            protected override void OnElementChanged(ElementChangedEventArgs<View> e)
            {
                base.OnElementChanged(e);
                if (e.OldElement != null)
                {
                    var nativeMap = Control as MKMapView;
                    nativeMap.GetViewForAnnotation = null;
                    nativeMap.CalloutAccessoryControlTapped -= OnCallourAccessoryControlTapped;
                    nativeMap.DidSelectAnnotationView -= OnDidSelect;
                    nativeMap.DidDeselectAnnotationView -= OnDidDeSelect;
                }
                if (e.NewElement != null)
                {
                    var formsMap = (CustomMap)e.NewElement;
                    var nativeMap = Control as MKMapView;
                    customPins = formsMap.CustomPins;
                    nativeMap.GetViewForAnnotation = GetViewForAnnotation;
                    nativeMap.CalloutAccessoryControlTapped += OnCallourAccessoryControlTapped;
                    nativeMap.DidSelectAnnotationView += OnDidSelect;
                    nativeMap.DidDeselectAnnotationView += OnDidDeSelect;
    
                }
    
            }
    
            private void OnDidDeSelect(object sender, MKAnnotationViewEventArgs e)
            {
                if (!e.View.Selected)
                {
                    customPinView.RemoveFromSuperview();
                    customPinView.Dispose();
                    customPinView = null;
    
                }
               
    
            }
    
            private void OnDidSelect(object sender, MKAnnotationViewEventArgs e)
            {
    
                throw new NotImplementedException();
            }
    
            private void OnCallourAccessoryControlTapped(object sender, MKMapViewAccessoryTappedEventArgs e)
            {
                throw new NotImplementedException();
            }
    
            protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
            {
             
                MKAnnotationView annotationView = null;
            
                if (annotation is MKUserLocation)
                    return null;
                var customPin = GetCustomPin(annotation as MKPointAnnotation);
                if (customPin == null)
                {
                    throw new Exception("not found");
                }
                annotationView = mapView.DequeueReusableAnnotation(customPin.Name);
                if (annotationView == null)
                {
                    annotationView = new CustomMKAnnotationView(annotation, customPin.Name);
                    annotationView.Image = UIImage.FromFile("pin.png");
           
                    annotationView.CalloutOffset = new CGPoint(0, 0);
                    annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromFile("monkey.png"));
                    annotationView.RightCalloutAccessoryView = UIButton.FromType(UIButtonType.DetailDisclosure);
                  
                    ((CustomMKAnnotationView)annotationView).Name = customPin.Name;
                    customPinView = new UIView();
                    var Label = new UILabel();
                    Label.Text = "Samsung";
                    Label.Frame=new CGRect(annotationView.GetFrame().X+35,annotationView.GetFrame().Y,100,50);
                    var Label2 = new UILabel();
                    Label2.Text = "20:20";
                    Label2.Frame = new CGRect(annotationView.GetFrame().X + 35, annotationView.GetFrame().Y+20, 100, 50);
                    customPinView.Frame= new CGRect(annotationView.GetFrame().X+40, annotationView.GetFrame().Y-20, 100, 50);
                    customPinView.AddSubview(Label);
                    customPinView.AddSubview(Label2);
                    Label.BaselineAdjustment = UIBaselineAdjustment.AlignBaselines;
                    customPinView.BackgroundColor = UIColor.White;
                    customPinView.Layer.CornerRadius = 5;
                    customPinView.Alpha = (nfloat)0.8;
                    customPinView.Layer.MasksToBounds = true;
                    annotationView.AddSubview(customPinView);
                }
                annotationView.CanShowCallout = true;
             
                return annotationView;
    
            }
    
            CustomPin GetCustomPin(MKPointAnnotation annotation)
            {
                var position = new Position(annotation.Coordinate.Latitude, annotation.Coordinate.Longitude);
                foreach (var pin in customPins)
                {
                    if (pin.Position == position)
                    { return pin; }
                }
                return null;
    
            }}
    

    结果:

    【讨论】:

    • 感谢您提供此示例!
    猜你喜欢
    • 2011-08-31
    • 2017-12-24
    • 2018-07-22
    • 2013-10-12
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多