iphone - 添加两个MKPolylineView

标签 iphone objective-c mapkit mkoverlay

我无法在 MKOverlay View 中添加两个具有不同颜色的不同 MKPolyline View 。关于如何实现这一目标有什么想法吗? 谢谢

这是我的代码:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView* overlayView = nil;
UIColor *mycolor;

self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];

mycolor = [UIColor colorWithRed:85.0/255.0 green:133.0/255.0 blue:255.0/255.0 alpha:0.6];
self.routeLineView.fillColor = mycolor;
self.routeLineView.strokeColor = mycolor;
self.routeLineView.lineWidth = 15;
[overlayView addSubview:self.routeLineView];

self.routeLineView2 = [[[MKPolylineView alloc] initWithPolyline:self.routeLine2] autorelease];
mycolor = [UIColor colorWithRed:85.0/255.0 green:19.0/255.0 blue:25.0/255.0 alpha:0.6];
self.routeLineView2.fillColor = mycolor;
self.routeLineView2.strokeColor = mycolor;
self.routeLineView2.lineWidth = 15;
[overlayView addSubview:self.routeLineView2];   

return overlayView;
}

最佳答案

对于添加到 map 的每个叠加层,将分别调用 viewForOverlay 方法。因此,在该方法中,您仅返回当前正在调用的覆盖层的覆盖层 View (即 overlay 参数)。

检查它正在请求哪个叠加层的 View ,并仅为该叠加层创建和返回 View 。

例如:

if (overlay == self.routeLine)
{
    //create and return overlay view for routeLine...
    //set color, etc...
    return self.routeLineView;
}
else
if (overlay == self.routeLine2)
{
    //create and return overlay view for routeLine2...
    //set color, etc...
    return self.routeLineView2;
}

return nil;

不要做任何addSubview的事情。只需创建覆盖 View 并返回它即可。

关于iphone - 添加两个MKPolylineView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10865894/

相关文章:

iphone - 为 UITableView 的第一个和最后一个单元格制作圆角

iphone - 应用内购买中的产品 ID 无效

objective-c - 显示 NSArrays 的 NSDictionary 最好的是什么?

ios - MKMapview注解动态pin图缩放后变化

iphone - 带有 subview 的 UIView beginAnimations

iphone - 如何从 CGPoint 或 CGPath 获取 UIBezierPath

ios - Sprite Kit - 节点离开屏幕的通知程序

ios - AFNetworking 3.0 无法下载图像

ios - 终止应用程序后我没有获取用户当前的运行位置

iphone - 跨多个 View 使用 CoreLocation 的最佳方式