2012-04-09 85 views
1

我試圖將用戶位置與選定註記的字幕在地圖視圖中的距離相加。它的機制正在起作用,但實際的標註在第一次顯示時會變得混亂。似乎有重繪問題。引腳上iOS SDK Mapview註釋標註重繪錯誤

mapView annotation redraw problem

隨後水龍頭顯示正確的佈局。

下面是相關代碼:

//選擇註釋

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ 

MKPointAnnotation *selectedAnnotation = view.annotation; 

//attempt to add distance on annotation 
CLLocation *pointALocation = [[CLLocation alloc] 
           initWithLatitude:selectedAnnotation.coordinate.latitude 
           longitude:selectedAnnotation.coordinate.longitude]; 
float distanceMeters = [pointALocation distanceFromLocation:locationManager.location]; 

//for sending info to detail 
myPinTitle = selectedAnnotation.title; 

[selectedAnnotation setSubtitle:[NSString stringWithFormat:@"%.2f miles away", (distanceMeters/1609.344)]]; 

} 我已經打過電話[查看setNeedsDisplay]當呼籲,但都無濟於事。

在此先感謝您的幫助。


奏效

這裏的解決方案是,我終於想出瞭解決方案。它似乎工作。

我從didSelectAnnotationView方法編輯了重複的代碼,上面,並想出了:

//called when user location changes 

- (void)updatePinsDistance 
{ 
for (int x=0; x< [[mapView annotations]count]; x++) { 
    MKPointAnnotation *thisPin =[[mapView annotations] objectAtIndex:x]; 

    //attempt to add distance on annotation 
    CLLocation *pointALocation = [[CLLocation alloc] 
           initWithLatitude:thisPin.coordinate.latitude 
           longitude:thisPin.coordinate.longitude]; 
    float distanceMeters = [pointALocation distanceFromLocation:locationManager.location]; 
    NSString *distanceMiles = [NSString stringWithFormat:@"%.2f miles from you", 
                (distanceMeters/1609.344)]; 

    [thisPin setSubtitle:distanceMiles]; 
    } 
} 

回答

3

你應該設置你的字幕在另一個地方比didSelectAnnotationView。實際上所有的annotationView都應該在它們被mapView:viewForAnnotation:方法返回之前設置它們的標題和副標題。

您設置長副標題的事實當然會解釋說明該標註尺寸不合適。在調用didSelectAnnotationView之前,必須計算大小。

+0

創建註釋時會添加原始字幕,但我想顯示從當前用戶位置到選定點的距離,因此我無法添加該信息。而且,替換的距離字符串實際上可能比原來的字符串小。 – 2012-04-09 18:05:47

+0

而應使用mapView:didUpdateUserLocation:delegate方法在用戶位置更改時重新計算它。坦率地說,在顯示標註的同時改變字幕並不是一件合理的事情。 – 2012-04-09 20:42:02

+0

因此,當用戶的位置發生變化時,是否應該更改每個註釋的副標題,即使它永遠不會被看到,而不僅僅是需要顯示的那個標題?我很困惑,因爲這更合理。你能詳細說明嗎?謝謝。 – 2012-04-09 22:14:32