Pizhev Racing
Pizhev Racing

Reputation: 486

How to set background color and change font color on Label in xamarin.forms.maps (iOS)?

Because I failed to insert two more lines in Info Window on the pin on xamarin.forms.maps on iOS project I managed to put them over Info Window like this:

Screen shot

In the android project I had two files with an extension .axml In which I added two <TextView/> and so I managed to add two more lines in InfoWindow on the pin.

But here I don't have (or don't know where) to add two more fields in the pin window itself so add them this way:

1. I add my objects who come from database with this code:

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("Custom pin not found");
        }

        annotationView = mapView.DequeueReusableAnnotation(customPin.Name);
        if (annotationView == null)
        {
            annotationView = new CustomMKAnnotationView(annotation, customPin.Name);
            //annotationView = new CustomMKAnnotationView(annotation, customPin.AlertLevel.ToString());
            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;
            ((CustomMKAnnotationView)annotationView).Url = customPin.Url;
            //Add First Line
            ((CustomMKAnnotationView)annotationView).AlertLevel = customPin.AlertLevel;
            //Add Second Line
            ((CustomMKAnnotationView)annotationView).CodeNum = customPin.CodeNum;
        }
        annotationView.CanShowCallout = true;

        return annotationView;
    }

2. I display the object with this code:

 void OnDidSelectAnnotationView(object sender, MKAnnotationViewEventArgs e)
    {
        CustomMKAnnotationView customView = e.View as CustomMKAnnotationView;
        customPinView = new UIView();

        if (customView.Name.Equals("Xamarin"))
        {
            customPinView.Frame = new CGRect(0, 0, 200, 84);
            //var image = new UIImageView(new CGRect(0, 0, 200, 84));
            //image.Image = UIImage.FromFile("xamarin.png");
            //customPinView.AddSubview(image);
            customPinView.Center = new CGPoint(0, -(e.View.Frame.Height + 75));

            //the first text to enter in info window
            var labelAlertLevel = new UILabel(new CGRect(60, 0, 200, 84));
            labelAlertLevel.Text = "Код на трвога (1-4): " + customView.AlertLevel.ToString();
            customPinView.AddSubview(labelAlertLevel);

            //the second text to enter in info window
            var labelCodeNum = new UILabel(new CGRect(60, 20, 200, 84));
            labelCodeNum.Text = "Код на станция: " + customView.CodeNum.ToString();
            customPinView.AddSubview(labelCodeNum);
            
            e.View.AddSubview(customPinView);
        }
    }

Basically I want to put them in the pin window, but if this is not possible how can i set their background and font color ?

I use this example.

Upvotes: 0

Views: 386

Answers (1)

Wen xu Li
Wen xu Li

Reputation: 1686

The following code includes normal display of information, custom background color and font color,I hope it can help you.

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)

            {

                return null;

            }

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

                ((CustomMKAnnotationView)annotationView).Name = customPin.Name;

                ((CustomMKAnnotationView)annotationView).Url = customPin.Url;

                ((CustomMKAnnotationView)annotationView).Description1 = customPin.Description1;

                ((CustomMKAnnotationView)annotationView).Description2 = customPin.Description2;

                ((CustomMKAnnotationView)annotationView).Description3 = customPin.Description3;

                annotationView.CanShowCallout = true;

            }

            configureDetailView(annotationView);

            return annotationView;

        }

        void configureDetailView(MKAnnotationView annotationView)

        {

            int width = 100;

            int height = 50;

            var snapshotView = new UIView();

            snapshotView.TranslatesAutoresizingMaskIntoConstraints = false;

            NSDictionary views = NSDictionary.FromObjectAndKey(snapshotView, new NSString("snapshotView"));

            snapshotView.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:[snapshotView(200)]", new NSLayoutFormatOptions(), null, views));

            snapshotView.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:[snapshotView(50)]", new NSLayoutFormatOptions(), null, views));

            var options = new MKMapSnapshotOptions();

            options.Size = new CGSize(width, height);

            options.MapType = MKMapType.SatelliteFlyover;

            options.Camera = MKMapCamera.CameraLookingAtCenterCoordinate(annotationView.Annotation.Coordinate, 250, 65, 0);

            var snapshotter = new MKMapSnapshotter(options);

            snapshotter.Start((snapshot, error) =>

            {

                if (snapshot != null)

                {

                    UILabel label = new UILabel();

                    UILabel label2 = new UILabel();

                    UILabel label3 = new UILabel();

                    label.Text = ((CustomMKAnnotationView)annotationView).Description1;

                    label2.Text = ((CustomMKAnnotationView)annotationView).Description2;

                    label3.Text = ((CustomMKAnnotationView)annotationView).Description3;

                    label2.BackgroundColor = UIColor.Blue;

                    label3.TextColor = UIColor.Red;

                    label.Frame = new CGRect(50, 0, 100, 15);

                    label2.Frame = new CGRect(50, 15, 100, 15);

                    label3.Frame = new CGRect(50, 30, 100, 15);

                    snapshotView.AddSubviews(label, label2, label3);

                }

            });
            annotationView.DetailCalloutAccessoryView = snapshotView;
        }


enter image description here

Upvotes: 3

Related Questions