Home > Back-end >  How to change pin icon on click in xamarin.forms.maps iOS c#
How to change pin icon on click in xamarin.forms.maps iOS c#

Time:10-19

I want to change pin icon when the pin is clicked.

I copy this code from my GetViewForAnnotation method in OnDidSelectAnnotationView but I don't know how to return annotationView.

I'm not sure if this is the right way, but can I get an example of how to change the icon when I click on the marker?

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

            customPinView.Center = new CGPoint(0, -(e.View.Frame.Height   75));

            e.View.AddSubview(customPinView);
        }

        var pin = GetCustomPin(c_annotation as MKPointAnnotation);
        int mapCode = customView.MapCode;
        var result = GetDataFromAPI(mapCode);
        var result2 = GetInfoFromStation(mapCode);

        MessagingCenter.Send<object, IEnumerable<AlertLevel>>(this, "PinSelected", result);
        MessagingCenter.Send<object, IEnumerable<StationInfoOnClick>>(this, "StationInfo", result2);


        MKAnnotationView annotationView = null;

        if (annotationView == null)
        {
            annotationView = new CustomMKAnnotationView(c_annotation, pin.Name);
            annotationView.CalloutOffset = new CGPoint(0, 0);
            ((CustomMKAnnotationView)annotationView).Name = pin.Name;
            ((CustomMKAnnotationView)annotationView).Url = pin.Url;
            ((CustomMKAnnotationView)annotationView).Address = pin.Address;
            //Add First Line
            ((CustomMKAnnotationView)annotationView).AlertLevel = pin.AlertLevel;

            if (pin.AlertLevel == 1)
            {
                annotationView.Image = UIImage.FromFile("red.png");
            }
            else if (pin.AlertLevel == 2)
            {
                annotationView.Image = UIImage.FromFile("yellow.png");
            }
            else if (pin.AlertLevel == 3)
            {
                annotationView.Image = UIImage.FromFile("orange.png");
            }
            else if (pin.AlertLevel == 4)
            {
                annotationView.Image = UIImage.FromFile("red.png");
            }

            //Add Second Line
            ((CustomMKAnnotationView)annotationView).CodeNum = pin.CodeNum;

            ((CustomMKAnnotationView)annotationView).MapCode = pin.MapCode;
        }

        annotationView.CanShowCallout = true;

        configureDetailView(annotationView);

        return annotationView;
    }

I try to change void OnDidSelectAnnotationView with MKAnnotationView OnDidSelectAnnotationView I receive error on this method:

protected override void OnElementChanged(ElementChangedEventArgs<View> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null)
        {
            var nativeMap = Control as MKMapView;
            nativeMap.GetViewForAnnotation = null;
            nativeMap.CalloutAccessoryControlTapped -= OnCalloutAccessoryControlTapped;
            nativeMap.DidSelectAnnotationView -= OnDidSelectAnnotationView;
            nativeMap.DidDeselectAnnotationView -= OnDidDeselectAnnotationView;
        }

        if (e.NewElement != null)
        {
            var formsMap = (CustomMap)e.NewElement;
            var nativeMap = Control as MKMapView;
            customPins = formsMap.CustomPins;

            nativeMap.GetViewForAnnotation = GetViewForAnnotation;
            nativeMap.CalloutAccessoryControlTapped  = OnCalloutAccessoryControlTapped;
            nativeMap.DidSelectAnnotationView  = OnDidSelectAnnotationView;
            nativeMap.DidDeselectAnnotationView  = OnDidDeselectAnnotationView;
        }
    }

The error is: Error CS0407: 'MKAnnotationView CustomMapRenderer.OnDidSelectAnnotationView(object, MKAnnotationViewEventArgs)' has the wrong return type (CS0407) (MaritsaTundzhaForecast.iOS)

Is there another way to change the map marker icon when clicking ?

This is my GetViewForAnnotation method:

protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
    {

        //от mainpage извикваш метода

        MKAnnotationView annotationView = null;

        if (annotation is MKUserLocation)
            return null;

        var customPin = GetCustomPin(annotation as MKPointAnnotation);

        //Get Value
        c_annotation = annotation;

        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.CalloutOffset = new CGPoint(0, 0);
            ((CustomMKAnnotationView)annotationView).Name = customPin.Name;
            ((CustomMKAnnotationView)annotationView).Url = customPin.Url;
            ((CustomMKAnnotationView)annotationView).Address = customPin.Address;
            //Add First Line
            ((CustomMKAnnotationView)annotationView).AlertLevel = customPin.AlertLevel;

            if (customPin.AlertLevel == 1)
            {
                annotationView.Image = UIImage.FromFile("green.png");
            }
            else if (customPin.AlertLevel == 2)
            {
                annotationView.Image = UIImage.FromFile("yellow.png");
            }
            else if (customPin.AlertLevel == 3)
            {
                annotationView.Image = UIImage.FromFile("orange.png");
            }
            else if (customPin.AlertLevel == 4)
            {
                annotationView.Image = UIImage.FromFile("red.png");
            }

            //Add Second Line
            ((CustomMKAnnotationView)annotationView).CodeNum = customPin.CodeNum;

            ((CustomMKAnnotationView)annotationView).MapCode = customPin.MapCode;
        }


        annotationView.CanShowCallout = true;

        configureDetailView(annotationView);

        return annotationView;
    }

I want to click to put the same icon with a different size.

I use this enter image description here

enter image description here

  • Related