我有一個IOS應用程序顯示地圖,並允許用戶從列表中選擇一個路線。選擇後,應用程序在地圖上繪製此路線。我正在爲我的路線點使用自定義圖片。我第一次運行我的應用程序並選擇路線時,地圖上的默認圖釘加載。如果我刷新我的地圖或選擇其他路線,我的自定義圖像顯示完美。這只是第一次,這是發生。我正在使用MKAnnotation,而不是MKPinAnnotationView,並且我確定沒有指定PinView的代碼。將不勝感激,任何幫助指出我在正確的方向來弄清楚這裏發生了什麼。謝謝!IOS引腳註釋最初顯示,然後使用自定義圖像後
這是我的自定義註解類
公共類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
{
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 Delegate類
公共覆蓋MKAnnotationView GetViewForAnnotation(的MKMapView MapView類,NSObject的註釋) {
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方法,其中i循環通過我的點,並把註釋 (點內是一選擇路線點對象的列表)
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再次調用。
顯示您用於設置註釋和刷新的代碼,並給出每個被調用時間的詳細信息。 – Wain
在GetViewForAnnotation中,ClosestStopAnnotation的if塊後有什麼代碼?最後是否有'返回null'?在該代碼中放置斷點或調試消息,以確保應用程序不會以某種方式創建意外類型的註釋。 – Anna