Xamarin iOS地图定制针脚

时间:2014-08-22 22:50:19

标签: xamarin

我使用Xamarin iOS MKMapView,我试图为每个引脚分别设置图像,但是当我覆盖GetViewForAnnotation方法时,它会使所有引脚都成为相同的图像,有没有办法让每个引脚都不同图像。

class MapDelegate : MKMapViewDelegate
        {
            static string annotationId = "CustomAnnotation";
            UIImageView venueView;
            UIImage venueImage;

            public override MKAnnotationView GetViewForAnnotation (MKMapView mapView, NSObject annotation)
            {
                MKAnnotationView annotationView = null;

                if (annotation is MKUserLocation)
                    return null; 

                if (annotation is CustomAnnotation) {

                    // show conference annotation
                    annotationView = mapView.DequeueReusableAnnotation (annotationId);

                    if (annotationView == null)
                        annotationView = new MKAnnotationView (annotation, annotationId);

                    UIImage image = ImageSource.FromFile("image.png");
                } 

                return annotationView;
            }

            public override MKOverlayView GetViewForOverlay (MKMapView mapView, NSObject overlay)
            {
                // return a view for the polygon
                MKPolygon polygon = overlay as MKPolygon;
                MKPolygonView polygonView = new MKPolygonView (polygon);
                polygonView.FillColor = UIColor.Blue;
                polygonView.StrokeColor = UIColor.Red;
                return polygonView;
            }
        }

构造函数:

map.MapType = MKMapType.Standard;
            map.ShowsUserLocation = true;
            map.ZoomEnabled = true;
            map.ScrollEnabled = true;

            map.DidUpdateUserLocation += (sender, ex) => {
                if (map.UserLocation != null) {
                    CLLocationCoordinate2D userLocation = map.UserLocation.Coordinate;
                    MKCoordinateSpan range = new MKCoordinateSpan(MilesToLatitudeDegrees(5), MilesToLongitudeDegrees(5, userLocation.Latitude));
                    MapDelegate mapDelegate = new MapDelegate();
                    map.Delegate = mapDelegate;

                    /*ADDING MULTIPLE ANNOTATIONS HERE*/
                        }
                    }
                }
            };

我想将每个注释设置为不同的自定义图像,有人可以帮助谢谢。

1 个答案:

答案 0 :(得分:0)

这可以通过使用Converter来实现,例如,您可以根据具体的引脚属性值指定具体图标。

private MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
    {
        MKAnnotationView annotationView = null;
        var formsMap = Element as MapWithPins;
        if (_customPins.Count == 0 || formsMap.PinsSource.Count != _customPins.Count)
        {
            //if (formsMap.PinsSource.Count != 0)
            _customPins = formsMap.PinsSource;
        }
        if (annotation is MKUserLocation)
            return null;
        if (!(annotation is MKPointAnnotation))
        {
            DrawMyLocationMarker(annotation, out annotationView);
        }
        else
        {
            var customPin = GetCustomPin(annotation as MKPointAnnotation);
            if (customPin == null)
                throw new Exception("Custom Pin not found");

            annotationView = mapView.DequeueReusableAnnotation(customPin.Guid.ToString());
            if (annotationView == null)
            {
                DrawMarkerForAnnotationView(annotation, out annotationView, customPin);
                if (customPin.Order > 0)
                    DrawNumber(customPin, annotationView);
            }
            else
                annotationView.Annotation = annotation;
        }
        return annotationView;
    }

如您所见,我根据属性绘制了不同的图标。如果我的pin的Order属性大于0,那么我在其上绘制订单号。否则,在DrawMarkerFirAnnotationView中,我有一个Converter,它根据属性返回正确的图标。

private static void DrawMarkerForAnnotationView(IMKAnnotation annotation, out MKAnnotationView annotationView,
        PinViewModel customPin)
    {
        annotationView = new MKAnnotationView(annotation, customPin.Guid.ToString());
        var contractor= new Contractor { ContractorType = customPin.ContractorType, CompetitorPriceLastDate = customPin.CompetitorPriceLastDate, HasActivity = customPin.HasActivity };
        annotationView.Image =
            UIImage.FromFile(
                new ContractorToIconConverter().Convert(contractor, typeof(ContractorType), null, null).ToString());
    }

我确定您注意到,图标基于ContractorType属性

public class BusRelToIconConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is Contractor)
        {
            var contractor= (Contractor)value;
            if (contractor.HasActivity)
            {
                if (busRel.IsVisitedToday())
                {
                    return "marker-yellow-visited.png";
                }
                else
                {
                    return "marker-yellow.png";
                }
            }
            else if (contractor.ContractorType == CustStatus.Competitor)
            {
                if (contractor.IsVisitedToday())
                {
                    return "marker-red-visited.png";
                }
                else
                {
                    return "marker-red.png";
                }
            }
            else
            {
                if (contractor.IsVisitedToday())
                {
                    return "marker-blue-visited.png";
                }
                else
                {
                    return "marker-blue.png";
                }
            }
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
祝你好运!