我試圖在我的地圖上顯示折線,但行不顯示。我嘗試了很多東西,但注意到似乎工作。iOS SDK:MapKit MKPolyLine未顯示
我檢查了核心數據函數,它正在返回數據,所以這不是問題。它必須在地圖上的某個地方(我猜)。 我確定它在某個地方肯定有點失誤,但我找不到它。
我的代碼:
- (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:'方法是什麼樣的? –
還要注意代碼是將一個lat/long分配給MKMapPoint。 MKMapPoint不是經度/緯度。使用CLLocationCoordinate2D而不是MKMapPoint,CLLocationCoordinate2DMake而不是MKMapPointMake和polylineWithCoordinates而不是polylineWithPoints。 – Anna
我將它改爲MKMapPoint,因爲我認爲那是問題所在。我將把它設置回CLLocationCoordinate2D。 我還添加了我的mapView:viewForOverlay。 –