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