Ios MKMapView上未显示MKPolyline

Ios MKMapView上未显示MKPolyline,ios,objective-c,mapkit,mkpolyline,Ios,Objective C,Mapkit,Mkpolyline,这是密码。这很直截了当。我正在为正在行走的人开辟道路。 下面是我的ViewController.m文件的代码: #import "ViewController.h" @interface ViewController () @property BOOL firstTime; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional

这是密码。这很直截了当。我正在为正在行走的人开辟道路。 下面是我的
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;
    }
}
#导入“ViewController.h”
@界面视图控制器()
@第一次的财产损失;
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
[self.mapView setDelegate:self];
[self.mapView设置显示位置:是];
[self.mapView setMapType:MKMapTypeHybrid];
[self-setLocationManager:[[CLLocationManager alloc]init]];
[self.locationManager setDelegate:self];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
[self.locationManager设置了所需的准确性:kClocationAccuracyBest];
[self.locationManager startUpdatingLocation];
self.index=0;
self.firstTime=是;
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(无效)位置管理器:(CLLocationManager*)管理器更新位置:(NSArray*)位置
{
如果(自我第一次)
{
CLLocation*startingLocation=[locations objectAtIndex:0];
self.startingpointcoridinates=startingLocation.coordinate;
self.index++;
MKPointAnnotation*startingPointAnnotation=[[MKPointAnnotation alloc]init];
startingPointAnnotation.title=@“起点”;
startingPointAnnotation.coordinate=startingLocation.coordinate;
[self.mapView addAnnotation:startingPointAnnotation];
self.firstTime=false;
}
[self.locations addObject:[locations objectAtIndex:0]];
CLLocationCoordinate2D坐标[[self.locations count]];
对于(int i=0;i
现在,只显示注释,没有显示任何
MKPolyline
。我做错了什么?谢谢

  • 如注释中所述,您的
    位置
    数组从未被分配和初始化,因此它是
    nil
    ,对它的调用(如
    addObject
    )不会执行任何操作,因此多段线从未获得任何添加到它的坐标(因此它不会显示)

    viewdiload
    中,在启动
    CLLocationManager
    之前,alloc和init数组:

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

    CLLocation *currentLocation = [locations objectAtIndex:i];
    
    这里,
    locations
    (不带
    self.
    )指的是委托方法的局部参数变量,而不是您的
    locations
    类实例级属性变量。编译器必须用类似“locations”的本地声明隐藏实例变量”的消息警告您

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

    因此,该行应更改为:

    CLLocation *currentLocation = [self.locations objectAtIndex:i];
    
    为了避免这些问题,最好使用与
    位置不同的名称

  • 正如在评论中提到的,由于每次用户移动时都要添加一个覆盖层,其中包含用户完整的运动轨迹,因此需要先删除上一个覆盖层。否则,将不必要地向贴图添加多个覆盖,因为最后一个覆盖覆盖了整个运动。前面的覆盖图不明显可见,因为它们具有相同的坐标、相同的颜色和相同的线宽。因此,在调用
    addOverlay
    之前,最简单的方法是使用map的当前覆盖列表调用
    removeOverlays

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

  • 最后,你可能有兴趣看看。他们的版本使用自定义覆盖,可以动态更新,而无需在每次更改或添加时删除和添加覆盖


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