【问题标题】:MKPolyline not showing on MKMapViewMKPolyline 未在 MKMapView 上显示
【发布时间】:2013-12-22 06:25:35
【问题描述】:

这是代码。它非常简单。我正在为正在行走的人开辟道路。 所以,这是我的ViewController.m 文件的代码:

#import "ViewController.h"

@interface ViewController ()

@property BOOL firstTime;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.mapView setDelegate:self];
    [self.mapView setShowsUserLocation:YES];
    [self.mapView setMapType:MKMapTypeHybrid];

    [self setLocationManager:[[CLLocationManager alloc] init]];
    [self.locationManager setDelegate:self];
    [self.locationManager setDistanceFilter:kCLDistanceFilterNone];
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.locationManager startUpdatingLocation];

    self.index = 0;

    self.firstTime = YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    if(self.firstTime)
    {
        CLLocation *startingLocation = [locations objectAtIndex:0];
        self.startingPointCooridinates = startingLocation.coordinate;
        self.index++;

        MKPointAnnotation *startingPointAnnotation = [[MKPointAnnotation alloc] init];
        startingPointAnnotation.title = @"Starting Point";
        startingPointAnnotation.coordinate = startingLocation.coordinate;

        [self.mapView addAnnotation:startingPointAnnotation];

        self.firstTime = false;
    }

    [self.locations addObject:[locations objectAtIndex:0]];

    CLLocationCoordinate2D coordinates[[self.locations count]];
    for(int i = 0; i < self.locations.count; i++)
    {
        CLLocation *currentLocation = [locations objectAtIndex:i];
        coordinates[i] = currentLocation.coordinate;
    }

    MKPolyline *pathPolyline = [MKPolyline polylineWithCoordinates:coordinates count:self.locations.count];
    [self.mapView addOverlay:pathPolyline];
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if([overlay isKindOfClass:[MKPolyline class]])
    {
        MKPolylineRenderer *polylineRenderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
        polylineRenderer.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
        polylineRenderer.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.7];
        polylineRenderer.lineWidth = 2.0;
        return polylineRenderer;
    }
    else
    {
        return  nil;
    }
}

现在,只有注释显示,没有任何MKPolyline 显示。我究竟做错了什么 ?谢谢。

【问题讨论】:

  • 您检查了 self.locations 的内容吗?坐标是否正确添加到 self.locations?据我所知,问题在于使用错误的计数参数调用 MKPolyline 的 polylineWithCoordinates 方法。
  • 你alloc+init self.locations了吗?你在 iOS 7 上运行吗?顺便说一句,您应该在添加之前执行 removeOverlays ,因为现在,代码正在添加一个新的叠加层,该叠加层比之前的部分长一个(绘制在最后一个之下)。不相关但请注意,fillColor 对折线没有影响(只需要 strokeColor)。
  • 正如 Anna 指出的那样,您必须设置 strokeColor 而不是 fillColor。
  • @Anna - 如何删除之前的叠加层?!
  • @Anna - 你是对的。我确实忘记alloc init位置数组。谢谢。但是如何删除最后一个叠加层?

标签: ios objective-c mapkit mkpolyline


【解决方案1】:
  1. 正如 cmets 中所述,您的 locations 数组永远不会被分配和初始化,因此它是 nil 并且调用它(如 addObject)什么都不做,因此折线永远不会添加任何坐标(等等它没有显示)。

    在viewDidLoad中,在启动CLLocationManager之前,分配并初始化数组:

    self.locations = [NSMutableArray array];
    
  2. 您将遇到的另一个问题是didUpdateLocations 中的这一行在for 循环中:

    CLLocation *currentLocation = [locations objectAtIndex:i];
    

    这里,locations(没有self.)指的是委托方法的局部参数变量,而不是你的locations类-实例级属性变量。编译器必须通过类似“'locations' 的本地声明隐藏实例变量”这样的消息来警告您。

    在这种情况下,警告很关键。您在这里真正要做的是引用 locations 属性变量,您将在其中存储用户的完整坐标轨迹,而不是仅具有最后 x 个未报告位置(通常只有 1 个对象)的局部变量。

    所以该行应该改为:

    CLLocation *currentLocation = [self.locations objectAtIndex:i];
    

    如果您只是使用与locations 不同的名称来避免这些问题,那就更好了。

  3. 正如 cmets 中也提到的,由于每次用户移动时您都添加了一个带有用户完整运动轨迹的叠加层,因此您需要先移除之前的叠加层。否则,您将不必要地向地图添加多个叠加层,因为最后一个叠加层覆盖了整个运动。之前的叠加层不明显可见,因为它们具有相同的坐标、相同的颜色和相同的线宽。所以在调用addOverlay 之前,最简单的方法是调用removeOverlays 并使用地图当前的叠加层列表:

    //remove any previous overlays first...
    [self.mapView removeOverlays:mapView.overlays];
    
    //now add overlay with the updated trail...
    [self.mapView addOverlay:pathPolyline];
    
  4. 不影响显示的一个小问题是为折线设置fillColor 无效。您只需要设置代码正在执行的strokeColor。您可以删除调用设置fillColor。

  5. 最后,您可能有兴趣查看Apple's Breadcrumb sample app。他们的版本使用自定义叠加层,可以动态更新,而无需在每次更改或添加时删除和添加叠加层。

【讨论】:

  • 非常感谢安娜!这简直太完美了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-11
  • 2015-08-26
  • 2011-01-31
  • 2016-05-20
相关资源
最近更新 更多