【发布时间】: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