我有一个 IOS 应用程序,它显示地图并让用户从列表中选择一条路线。选择后,应用程序会在地图上绘制这条路线。我正在为我的路线点使用自定义图像。我第一次运行我的应用程序并选择一条路线时,默认图钉会加载到地图上。如果我刷新我的地图或选择另一条路线,我的自定义图像会完美显示。只有在第一次运行时才会发生这种情况。我使用的是 MKAnnotation,而不是 MKPinAnnotationView,并且我确保没有指定 PinView 的代码。任何帮助我指出正确的方向以弄清楚这里发生了什么,我们将不胜感激。谢谢!
这些是我的自定义注释类
公共类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 委托类中
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 对象的列表)
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。