2010-09-29 82 views
19

我想製作一張地圖,在那裏我可以看到我目前的位置,看看這條街叫什麼名字。ViewForAnnotation不被稱爲

到目前爲止,我可以在我的地圖上放一個圖釘,但由於某種原因,我沒有收到標註。 和我已經在我的viewForAnnotation方法中放了一個NSLog,但它沒有被調用,所以我無法測試它。

有人可以幫我嗎?

-(void)lat:(float)lat lon:(float)lon 
{ 
    CLLocationCoordinate2D location; 
    location.latitude = lat; 
    location.longitude = lon; 
    NSLog(@"Latitude: %f, Longitude: %f",location.latitude, location.longitude); 
    //One location is obtained.. just zoom to that location 

    MKCoordinateRegion region; 
    region.center=location; 
    //Set Zoom level using Span 
    MKCoordinateSpan span; 
    span.latitudeDelta=.005f; 
    span.longitudeDelta=.005f; 
    region.span=span; 
    [map setRegion:region animated:TRUE]; 

    //MKReverseGeocoder *geocoder=[[MKReverseGeocoder alloc] initWithCoordinate:location]; 
    //geocoder.delegate=self; 
    //[geocoder start]; 
    if (cPlacemark != nil) { 
     [map removeAnnotation:cPlacemark]; 
    } 
    cPlacemark=[[CustomPlacemark alloc] initWithCoordinate:location]; 
    cPlacemark.title = mPlacemark.thoroughfare; 
    cPlacemark.subtitle = mPlacemark.locality; 
    [map addAnnotation:cPlacemark]; 
    [cPlacemark release]; 

    [mLocationManager stopUpdatingLocation]; 
} 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
if ([annotation isKindOfClass:[MKUserLocation class]]) 
    return nil; 

// try to dequeue an existing pin view first 
if ([annotation isKindOfClass:[CustomPlacemark class]]){ 
MKPinAnnotationView *pinView=(MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:@"customIdentifier"]; 

if (!pinView) 
{ 
    // if an existing pin view was not available, create one 
    MKPinAnnotationView* cPinAnnoView = [[[MKPinAnnotationView alloc] 
              initWithAnnotation:annotation reuseIdentifier:@"customIdentifier"] autorelease]; 
    cPinAnnoView.pinColor = MKPinAnnotationColorPurple; 
    cPinAnnoView.animatesDrop = YES; 
    cPinAnnoView.canShowCallout = YES; 
    // Add button 
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [leftButton addTarget:self action:@selector(annotationViewClick:) forControlEvents:UIControlEventTouchUpInside]; 
    cPinAnnoView.leftCalloutAccessoryView = leftButton; 

} else 
    { 
     pinView.annotation = annotation; 
    } 
    return pinView; 
} 
return nil; 

}

現在,我已經定製了自己的viewForAnnotation是這樣。 但我仍然無法從我的引腳獲得標註,並且引腳保持紅色。 但它應該是什麼都沒有的紫色

+3

如果你的地圖是從一個筆尖加載的,你記得把它連接起來嗎?你記得設置代表嗎? – 2010-09-29 12:32:25

+0

我的委託人「連線」到我的搜索欄和searchdisplaycontroller它現在正在工作:P感謝我仍然需要弄清楚爲什麼我沒有得到標註 – Nico 2010-09-29 12:36:40

+0

你可以發佈你的'CustomPlacemark'類嗎?我看不出任何理由爲什麼這不起作用... – 2010-10-01 12:29:51

回答

5

難道是你的註釋已被添加到MKMapView的當前視圖區域之外嗎?

+0

你看到的代碼是我所有的註釋 – Nico 2010-09-29 12:29:54

+1

我upvoted這是因爲如果你認爲你已經正確連線所有東西,請仔細檢查你的經度和緯度值:也許你讓它們顛倒過來,或者當它們應該是負數時,讓它們中的一個(或兩個)都爲正。或者,在您的代碼中,由於剪切/粘貼作業潦草,您在lat和lon都使用lat。 =) – UXUiOS 2014-08-04 16:43:11

7

爲了獲得viewForAnnotation被調用,請將mapView.delegate=self;添加到例如該viewDidLoad方法:

- (void)viewDidLoad { 

    [super viewDidLoad]; 
    mapView.delegate=self; 
} 
+2

iOS 6(http://openradar.appspot.com/12346693)存在一個錯誤 - 如果您在添加註釋之後設置委託,它們將獲得默認註釋視圖,並且自定義mapView: viewForAnnotation不會被調用。 – vadimtrifonov 2013-08-19 21:46:20

32

我這是沒有的MapView委託設置爲文件所有者同樣的問題。

  1. 打開你的筆尖
  2. 右鍵點擊的MapView
  3. 拖動委託文件的所有者
+0

謝謝!這真的有用。 – 2012-04-26 17:19:25

+4

或者如果使用故事板 - CTRL單擊MapView並拖動到橙色ViewController圖標(在窗口正下方的黑條上) – Tim 2012-06-18 15:29:23

1

其他人已經解釋,賠率是很高,你有沒有連接你的MapView委託給你的控制器。它首先要檢查

23

我有同樣的問題,正如你所提到的。代表已被設置爲ViewController,但viewForAnnotation選擇器未被調用。一些檢查後,我意識到,如果你沒有在主線程中調用addAnotation,MapView的就不叫viewForAnnotation,所以以下更新解決了我的問題:

前:

[_mMapView addAnnotation:marker]; 

後:

dispatch_async(dispatch_get_main_queue(), ^{ 
    [_mMapView addAnnotation:marker]; 
}); 
+0

您的回答確實解決了我的問題;) – JackieLam 2014-02-20 11:30:11

+0

這對我也有幫助。奇怪的是,它不是第一次工作,因爲它在其他viewController中以'before'的方式工作。 :) – DevC 2014-08-14 08:30:58

+1

@DevC可能在其他地方你的代碼運行在UI線程(又名主線程)。很高興幫助:-) – 2014-08-14 14:43:48

2

對於故事板,Ctl將MKMapView拖動到ViewController底部欄上的橙色圓圈,然後選擇委託。

這將解決問題。

1

正如評論中提到的vatrif,您必須設置您的代理之前添加批註到您的MKMapView對象。

0

我一直在ios 9 Mapview相關的應用程序工作,我遇到了同樣的問題。

不知何故,我解決了我的問題,在我的情況下im調整mapview。 我在調整mapview的大小後添加了delegate。它現在完美的工作。

0

在設置了mapview的委託後,如果仍然沒有調用viewforannotation,那麼這是你錯過的 - 在接口生成器中將self.mapView.showsUserLocation設置爲YES,你可以在屬性中勾選shows userLocation選項督察。