【发布时间】:2013-05-30 14:04:57
【问题描述】:
我试图在我的地图上显示一条折线,但这条线没有显示出来。我尝试了很多东西,但注意到似乎有效。
我检查了 Core Data 函数,它正在返回数据,所以这不是问题。它必须是我在地图点创建或地图上的某个地方(我猜)。 我确定它一定是某个地方的一个小错误,但我找不到它。
我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
mapView.delegate = self;
}
- (void)createLine
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Logs" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSError *error;
NSArray *logs = [context executeFetchRequest:request error:&error];
int logsCount = [logs count];
MKMapPoint points[logsCount];
// loop logs
for (int i = 0; i < logsCount; i++)
{
MKMapPoint point;
point = MKMapPointMake([[[logs objectAtIndex:i] valueForKey:@"lat"] doubleValue], [[[logs objectAtIndex:i] valueForKey:@"lng"] doubleValue]);
points[i] = point;
}
MKPolyline *routeLine = [MKPolyline polylineWithPoints:points count:logsCount];
[mapView addOverlay:routeLine];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView *mapOverlayView = [[MKOverlayView alloc] initWithOverlay:overlay];
return mapOverlayView;
}
【问题讨论】:
-
你的
mapView:viewForOverlay:方法是什么样的? -
另请注意,代码将纬度/经度分配给 MKMapPoint。 MKMapPoint 不是纬度/经度。使用 CLLocationCoordinate2D 代替 MKMapPoint,使用 CLLocationCoordinate2DMake 代替 MKMapPointMake,使用 polylineWithCoordinates 代替 polylineWithPoints。
-
我将其更改为 MKMapPoint,因为我认为这是问题所在。我将它设置回 CLLocationCoordinate2D。我还添加了我的 mapView:viewForOverlay。
标签: ios objective-c ios6 mapkit