How can we add multiple image to the map according to the status on the pin list

ShamnadShamnad USMember ✭✭✭

I have a custom map implementation in xamrin.froms . am able to customize the pin icon to custom icons (image). i have different pins and those pin have different icons . am not able to bind the different icons to the map. Its always same icons even i have added the conditions .

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

        customPins.ToList().ForEach(x =>{

            switch ((x.PinType){
                case PinTypeOptions.Vehicle:
                    marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.Truck50));
                    break;
                case PinTypeOptions.OperationLocation:
                    marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.feuerwehrauto100));

                    break;
                default:
                    break;
            }                
        });                
        return marker;
    }'

Best Answer

  • JoeMankeJoeManke US ✭✭✭✭✭
    Accepted Answer

    You're looping through the entire custom pin list for each marker, which ends up setting the marker icon to whatever the PinType is for the last pin in the list. Just check the PinType on the pin that you get as a parameter to the method.

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    Accepted Answer

    You're looping through the entire custom pin list for each marker, which ends up setting the marker icon to whatever the PinType is for the last pin in the list. Just check the PinType on the pin that you get as a parameter to the method.

  • ShamnadShamnad USMember ✭✭✭

    @JoeManke thanks for your quick response. you mean the enum PinType ? which is already defined right.
    i have some custom status for each object inside the custom pin list. in this case how can we handle . lets say i have status called A, B ,C, D each status have its own image also.
    thanks in advance

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    I don't know how you define your custom pins but there must be some way to associate the Xamarin.Forms Pin that you're getting in that method with one of your custom pins. Your switch statement is fine, you just need to find the correct custom pin instead of looping through the whole list.

  • ShamnadShamnad USMember ✭✭✭
    edited August 5

    @JoeManke its resolved i got a way, i have defined a custom pin model and inside that i created the custom enum. thanks for your help

  • ShamnadShamnad USMember ✭✭✭

    @JoeManke am facing the same problem in iOS also .any idea

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    The same problem where you're doing an unnecessary loop, or do you not know where/how to set the icon?

  • ShamnadShamnad USMember ✭✭✭

    @JoeManke I tried this way but not working can you help to find my mistake ?

    'protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
    {
    MKAnnotationView annotationView = null;

        if (annotation is MKUserLocation)
            return null; 
        var customPin = GetCustomPin(annotation as MKPointAnnotation); 
        var test = "abc";
       // annotationView = new CustomMKAnnotationView(annotation, customPin.image);
        if (annotationView == null)
        {             
    
                annotationView = new CustomMKAnnotationView(annotation, customPin.image);
                 //annotationView.Image = UIImage.FromFile(customPin.image);
    
                if (customPin.image == "status.png")
                {
                   annotationView.Image = UIImage.FromFile( " image1.png");
                }
                else
                {
                       annotationView.Image = UIImage.FromFile("image2.png");
                }
                annotationView.CalloutOffset = new CGPoint(0, 0);
                annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromFile("monkey.png"));
                annotationView.RightCalloutAccessoryView = UIButton.FromType(UIButtonType.DetailDisclosure);
            }
            annotationView.CanShowCallout = true;
            return annotationView;   
    } '
    

    this is my code

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    That code looks fine, except for the extra space in the string " image1.png"

Sign In or Register to comment.