IOS pin annotation shows initially, then custom images are used after

廉价感情. 提交于 2019-12-11 04:49:23

问题


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

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; 

    }
}

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; 

    }
}

This is in my mapview Delegate class

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;
        }

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++;
    }
     }

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


回答1:


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!



来源:https://stackoverflow.com/questions/17531119/ios-pin-annotation-shows-initially-then-custom-images-are-used-after

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!