最初显示IOS引脚注释,然后使用自定义图像 [英] IOS pin annotation shows initially, then custom images are used after

查看:82
本文介绍了最初显示IOS引脚注释,然后使用自定义图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个IOS应用程序,可以显示地图,并允许用户从列表中选择路线.选择后,应用程序会在地图上绘制此路线.我正在为路线点使用自定义图像.第一次运行我的应用程序并选择路线时,默认图钉会加载到地图上.如果我刷新地图或选择其他路线,我的自定义图像会完美显示.这只是在第一次运行时才发生.我使用的是MKAnnotation,而不是MKPinAnnotationView,并且我确保没有指定PinView的代码.任何帮助我指出正确方向以找出此处发生的情况的帮助将不胜感激.谢谢!

I have an IOS app that shows the map and lets users select a route from a list. Upon selection, the app draws this route on the map. I am using a custom image for my route points. The first time i run my app and select a route, the default pushpins load on the map. If i refresh my map or select another route, my custom images appear perfectly. It is only on the first run that this is happening. I am using MKAnnotation, not MKPinAnnotationView, and I have made sure there is no code specifying a PinView. Any help in pointing me in the right direction to figuring out what's happening here would be appreciated. Thanks!

这些是我的自定义注释类

These are my custom annotation classes

公共类RouteAnnotation:MKOverlay

public class RouteAnnotation : MKOverlay

{
    private CLLocationCoordinate2D _coordinate;
    private string _title;
    public override CLLocationCoordinate2D Coordinate {
        get { return _coordinate; }
        set { _coordinate = value; }
    }
    public override string Title {
        get { return _title; }
    }


    public RouteAnnotation (CLLocationCoordinate2D coord,
                            string t) : base()
    {
        _coordinate=coord;
        _title=t; 

    }
}

公共类ClosestStopAnnotation:MKOverlay

public class ClosestStopAnnotation : MKOverlay

{

            private CLLocationCoordinate2D _coordinate;
    private string _title;

    public override CLLocationCoordinate2D Coordinate {
        get { return _coordinate; }
        set { _coordinate = value; }
    }

    public override string Title {
        get { return _title; }
    }


    public ClosestStopAnnotation (CLLocationCoordinate2D coord,
                            string t) : base()
    {
        _coordinate=coord;
        _title=t; 

    }
}

这是在我的mapview委托类中

This is in my mapview Delegate class

公共重写MKAnnotationView GetViewForAnnotation(MKMapView mapView,NSObject注释) {

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

        MKAnnotationView anView;

        if (annotation is MKUserLocation) {
            return null; 
        }

        if (annotation is RouteAnnotation) {


            anView = (MKAnnotationView)mapView.DequeueReusableAnnotation (ranv);

            if (anView == null)
                anView = new MKAnnotationView (annotation, ranv);

            anView.Image = UIImage.FromFile ("stop20.png");
            anView.CanShowCallout = true;
            return anView;
        }
        if (annotation is ClosestStopAnnotation) {

            anView = (MKAnnotationView)mapView.DequeueReusableAnnotation (canv);


            if (anView == null)
                anView = new MKAnnotationView (annotation, canv);

            anView.Image = UIImage.FromFile ("closeststop40.png");
            anView.CanShowCallout = true;
            anView.Selected = true;
            return anView;
        }

在我的视图控制器中,在名为GetClosestStop的方法内,在其中循环遍历我的点并放置注释 (点是RoutePoint对象的列表)

And in my view controller, inside a method called GetClosestStop, where i loop through my points and place the annotations (points is a list of RoutePoint objects)

     foreach (RoutePoint p in points) {
    if (p.Latitude != response.Latitude && p.Longitude != response.Longitude) {
    String pName = p.Name;
    var stopCoord = new CLLocationCoordinate2D (p.Latitude, p.Longitude);
    RouteAnnotation stop = new RouteAnnotation (stopCoord, pName);
    stops [i] = stopCoord;
    mapView1.AddAnnotation (stop);
    anns [i] = stop;
    i++;
            }
            else {
     coord = new CLLocationCoordinate2D (response.Latitude, response.Longitude);
     close = new ClosestStopAnnotation (coord, title);
     mapView1.AddAnnotation (close);
     mapView1.SelectAnnotation(close, false);
     stops [i] = coord;
     i++;
    }
     }

每当单击刷新按钮或选择其他路线时,都会清除地图并再次调用GetClosestStop.

Whenever the refresh button is clicked, or another route is selected, the map is cleared and GetClosestStop is called again.

推荐答案

经过大量调试和尝试了不同的方法之后,我意识到直到循环注释并添加注释之后,才分配mapView委托.这就是为什么仅在第一次运行时使用默认图钉的原因.感谢您的帮助!

After much debugging and trying different approaches, I realized that my mapView Delegate was not being assigned until after the annotations were looped though and added. This is why the default pushpins were only used on the first run through. Thanks for the help!

这篇关于最初显示IOS引脚注释,然后使用自定义图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆