【问题标题】:Custom pin on xamarin forms mapxamarin 表单地图上的自定义引脚
【发布时间】:2015-12-02 17:31:07
【问题描述】:

我有一个需要自定义地图图钉的 Xamarin Forms 项目。我在 PCL 中定义了存根,并在继承地图的类中定义了几个附加属性(用于纬度和经度)。

地图显示正常,但自定义图像图钉仅显示为标准红色图钉。

以下是我的地图自定义渲染器(iOS 版本)。从我在各种论坛上看到的情况来看,这应该可以正常工作。

public class CustomMapRenderer : ViewRenderer<CustomMap, MKMapView>
{
    MKMapView mkMapView;

    protected override void OnElementChanged(ElementChangedEventArgs<CustomMap> e)
    {
        base.OnElementChanged(e);
        var map = e.NewElement;

        SetNativeControl(new MKMapView(CGRect.Empty));
        mkMapView = Control;

        MyMapDelegate myMapDelegate = new MyMapDelegate();
        mkMapView.Delegate = myMapDelegate;
        mkMapView.AddAnnotation(new MKPointAnnotation()
            {
                Coordinate = new CLLocationCoordinate2D(map.MapPinLatitude, map.MapPinLongitude)
            });
        mkMapView.MapType = MKMapType.Hybrid;
        mkMapView.ZoomEnabled = true;
    }
}

public class MyMapDelegate : MKMapViewDelegate
{
    protected string annotationIdentifier = "PinAnnotation";

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

        MKAnnotationView anView;

        if (annotation is MKUserLocation)
            return null; 

        // create pin annotation view
        anView = (MKPinAnnotationView)mapView.DequeueReusableAnnotation(annotationIdentifier);

        if (anView == null)
            anView = new MKPinAnnotationView(annotation, annotationIdentifier);

        anView.Image = GetImage("pinned_location.png");
        anView.CanShowCallout = true;

        return anView;
    }

    public UIImage GetImage(string imageName)
    {
        var image = UIImage.FromFile(imageName).Scale(new SizeF() { Height = 20, Width = 30 });

        return image;
    }

谁能告诉我为什么我没有看到图钉的自定义图像?

【问题讨论】:

    标签: xamarin xamarin.ios xamarin.forms


    【解决方案1】:

    您应该使用MKAnnotationView 而不是MKPinAnnotationView

    // create annotation view
    anView = (MKAnnotationView)mapView.DequeueReusableAnnotation(annotationIdentifier);
    
    if (anView == null)
        anView = new MKAnnotationView(annotation, annotationIdentifier);
    

    MKPinAnnotationView 仅用于默认图标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-14
      • 2019-07-17
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      相关资源
      最近更新 更多