2013-05-30 44 views
2

我試圖在我的地圖上顯示折線,但行不顯示。我嘗試了很多東西,但注意到似乎工作。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; 
} 
+0

你的'mapView:viewForOverlay:'方法是什麼樣的? –

+0

還要注意代碼是將一個lat/long分配給MKMapPoint。 MKMapPoint不是經度/緯度。使用CLLocationCoordinate2D而不是MKMapPoint,CLLocationCoordinate2DMake而不是MKMapPointMake和polylineWithCoordinates而不是polylineWithPoints。 – Anna

+0

我將它改爲MKMapPoint,因爲我認爲那是問題所在。我將把它設置回CLLocationCoordinate2D。 我還添加了我的mapView:viewForOverlay。 –

回答

2

有兩個問題與代碼所示:正在使用MKMapPoint S的被錯誤地設置爲緯度/經度值創建

  1. 的折線。 MKMapPoint不是緯度/經度。它是平面地圖投影上經緯度的x/y變換。 的經緯度值轉換爲MKMapPoint s,使用MKMapPointForCoordinate或僅使用CLLocationCoordinate2D代替。只要使用CLLocationCoordinate2D當你有lat/long時更容易編碼和理解。
  2. viewForOverlay,代碼創建一個空的MKOverlayView這是不可見的。改爲創建一個MKPolylineView(繪製MKPolyline s的子類MKOverlayView)並設置其strokeColor


對於第一個問題,使用:

  • CLLocationCoordinate2D代替MKMapPoint
  • CLLocationCoordinate2DMake代替MKMapPointMake
  • ,取而代之的polylineWithCoordinatespolylineWithPoints


對於第二個問題,這裏有一個例子:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
{ 
    if ([overlay isKindOfClass:[MKPolyline class]]) 
    { 
     MKPolylineView *mapOverlayView = [[MKPolylineView alloc] initWithPolyline:overlay]; 
     //add autorelease if not using ARC 
     mapOverlayView.strokeColor = [UIColor redColor]; 
     mapOverlayView.lineWidth = 2; 
     return mapOverlayView; 
    } 

    return nil; 
} 


一對夫婦的其他東西:

  • 相反的valueForKey:@"lat",我會用objectForKey:@"lat"(同爲@"lng")。
  • 確保已設置地圖視圖的delegate,否則即使進行所有其他更改,也不會調用viewForOverlay委託方法。
+0

非常廣泛的答案謝謝!它正在工作:-) 一注:objectForKey:@「」不工作(得到一個錯誤,說對象上沒有這樣的選擇器),所以我使用valueForKey:@「」。爲什麼要使用objectForKey而不是valueForKey? –

+0

我以爲你在NSDictionary上調用它。請參閱http://stackoverflow.com/questions/1062183/difference-between-objectforkey-and-valueforkey。 – Anna