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

  • Question

  • User50092 posted

    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;
        }'
    
    Monday, August 5, 2019 4:54 PM

Answers

  • User53115 posted

    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.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, August 5, 2019 5:08 PM

All replies

  • User53115 posted

    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.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, August 5, 2019 5:08 PM
  • User50092 posted

    @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

    Monday, August 5, 2019 5:33 PM
  • User53115 posted

    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.

    Monday, August 5, 2019 6:37 PM
  • User50092 posted

    @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

    Monday, August 5, 2019 6:48 PM
  • User50092 posted

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

    Wednesday, August 14, 2019 12:23 PM
  • User53115 posted

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

    Wednesday, August 14, 2019 4:46 PM
  • User50092 posted

    @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

    Friday, August 16, 2019 5:15 AM
  • User53115 posted

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

    Friday, August 16, 2019 1:46 PM