我正在将两个不同的MKGeodesicPolyline实例添加到这样的MKMapView中
CLLocation *LAX = [[CLLocation alloc] ...];
CLLocation *JFK = [[CLLocation alloc] ...];
CLLocation *LHR = [[CLLocation alloc] ...];
CLLocationCoordinate2D laxToJfkCoords[2] = {LAX.coordinate, JFK.coordinate};
CLLocationCoordinate2D jfkToLhrCoords[2] = {JFK.coordinate, LHR.coordinate};
MKGeodesicPolyline *laxToJfk = [MKGeodesicPolyline polylineWithCoordinates:laxToJfkCoords count:2];
MKGeodesicPolyline *jfkToLhr = [MKGeodesicPolyline polylineWithCoordinates:jfkToLhrCoords count:2];
[mapView addOverlay:laxToJfk];
[mapView addOverlay:jfkToLhr];我希望用需要在rendererForOverlay委托方法中配置的不同样式呈现这两种覆盖。
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay {
if (![overlay isKindOfClass:[MKPolyline class]]) {
return nil;
}
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:(MKPolyline *)overlay];
renderer.lineWidth = 3.0f;
// How to set different colors for LAX-JFK and JFK-LHR?
renderer.strokeColor = [UIColor blueColor];
return renderer;
}我的问题是,在上述方法中,有哪些选择可以识别这两种不同的覆盖?
到目前为止,我考虑的是:
MKGeodesicPolyline是通过静态工厂方法初始化的。overlay参数与这些参数进行比较。这确实有效,但感觉有点笨拙。另外,对于两个以上的覆盖,这种方法需要通过使用NSSet或NSArray来扩展。我还能做些什么来简化这个程序吗?MKGeodesicPolyline似乎没有任何可用于标记的属性。
发布于 2015-09-02 10:03:52
子类的一个替代方法是使用关联对象。但它的使用往往是不受欢迎的。
一个更长但更稳定的解决方案是制作一个自定义MKOverlay和一个MKOverlayRenderer,分别将它们的大部分实现转发给MKGeodesicPolyline和MKPolylineRenderer的私有实例。然后可以添加一个自定义属性来设置颜色。
https://stackoverflow.com/questions/32348942
复制相似问题