我有一个显示 map 并允许用户从列表中选择路线的 IOS 应用程序。选择后,应用程序会在 map 上绘制这条路线。我正在为我的路线点使用自定义图像。我第一次运行我的应用程序并选择一条路线时, map 上会加载默认图钉。如果我刷新我的 map 或选择另一条路线,我的自定义图像会完美显示。只有在第一次运行时才会发生这种情况。我使用的是 MKAnnotation,而不是 MKPinAnnotationView,并且我已确保没有指定 PinView 的代码。任何帮助我指出正确方向以弄清楚这里发生的事情的任何帮助都将不胜感激。谢谢!
这些是我的自定义注释类
公共(public)类 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)类 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)类中
public override 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;
}
在我的 View Controller 中,在一个名为 GetClosestStop 的方法中,我在其中循环遍历我的点并放置注释 (points 是 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++;
}
}
每当单击刷新按钮或选择另一条路线时, map 将被清除并再次调用 GetClosestStop。
最佳答案
经过多次调试和尝试不同的方法后,我意识到我的 mapView 委托(delegate)直到注释被循环并添加后才被分配。这就是默认图钉仅在第一次运行时使用的原因。感谢您的帮助!
关于最初显示 IOS pin 注释,然后使用自定义图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17531119/