如何在viewForOverlay中区分两个MKPolyline: [英] How to distinguish two MKPolyline's in viewForOverlay:
问题描述
我将多个 MKPolylines
作为重叠
加载到 MKMapView
。我希望能够区分这些如何改变颜色,线宽等等。
I am loading multiple MKPolylines
as overlays
onto an MKMapView
. I would like to be able to distinguish these some how so change things like color, line width, etc.
但是,当 viewForOverlay:
被调用,它看到所有 MKPolylines
相同,这不允许我更改任何一个。
But, when viewForOverlay:
gets called, it sees all my MKPolylines
the same, which doesn't allow me to change any of them.
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolylineView *aView = [[[MKPolylineView alloc] initWithPolyline:(MKPolyline*)overlay] autorelease];
aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
MKZoomScale currentZoomScale = (CGFloat)(mapView.bounds.size.width / mapView.visibleMapRect.size.width);
aView.lineWidth = MKRoadWidthAtZoomScale(currentZoomScale);
return aView;
}
// Want to color my next overlay red
return nil;
}
我该怎么做?我可以以某种方式将标签附加到每个 MKPolyline
吗?或者,另一种更好的方法吗?
How can I do this? Could I somehow attach tag to each MKPolyline
? Or, another, better way to do this?
推荐答案
MKPolyline
继承来自 MKShape
,其中有一个可设置的标题
(以及字幕
)您可以用来告诉他们的财产。
MKPolyline
inherits from MKShape
which has a settable title
(and subtitle
) property which you can use to tell them apart.
这个答案有一个如何与 MKPolygon
对象一起使用的例子。
This answer has an example of how to use it with MKPolygon
objects.
如果 title
和 subtitle
不足以满足您的要求,那么您可以将子类命名为Mundi。
If title
and subtitle
are not sufficient for your requirements, then you can subclass as Mundi commented.
这篇关于如何在viewForOverlay中区分两个MKPolyline:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!