2012-05-22 16 views
3

我有一個地圖視圖,並且有10個存儲位置數據通過webservice傳遞。我只想推入我的詳細視圖以顯示點擊商店的地址,電話和其他信息。當在地圖上點擊註解時將數據傳遞到detailView

當用戶點擊或觸摸mapkit上的註釋時,我需要將數據傳遞給我的detailview。我的mapview中有10個註釋,首先我想知道,我該如何理解或如何獲得單擊註釋的註釋ID?

這是我的方法返回引腳

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 

    if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; 

    static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 

    MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier]; 

    pinView.animatesDrop=YES; 
    pinView.canShowCallout=YES; 
    pinView.pinColor=MKPinAnnotationColorPurple; 

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 

    [rightButton addTarget:self 
    action:@selector(showDetails:) 
    forControlEvents:UIControlEventTouchUpInside]; 

    pinView.rightCalloutAccessoryView = rightButton; 

    return pinView; 
} 
    /* and my action method for clicked or tapped annotation: */ 

- (IBAction)showDetails:(id)sender{ 

     NSLog(@"Annotation Click"); 

     [[mtMap selectedAnnotations]objectAtIndex:0]; 
     magazaDetayViewController *detail = [[magazaDetayViewController 
     alloc]initWithNibName:@"magazaDetayViewController" bundle:nil]; 

     [email protected]""; 
     [email protected]""; 
     [email protected]""; 
     [email protected]""; 
     [email protected]""; 
     [self.navigationController pushViewController:detail animated:YES]; 

} 

如果我可以獲取單擊註解索引沒有我可以用我的陣列填寫詳細屬性。 如果這是不可能的有沒有其他的方式來做到這一點?

回答

9

首先在你的annotaio ñ視圖delegat讓一個按鈕在詳細視圖去像波紋管:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ 

MKPinAnnotationView *mypin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"current"]; 
mypin.pinColor = MKPinAnnotationColorPurple; 
mypin.backgroundColor = [UIColor clearColor]; 
UIButton *goToDetail = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
mypin.rightCalloutAccessoryView = myBtn; 
mypin.draggable = NO; 
mypin.highlighted = YES; 
mypin.animatesDrop = TRUE; 
mypin.canShowCallout = YES; 
return mypin; 
} 

現在使用下面的委託每當annotationView按鈕將得到挖掘以下代表將從稱爲在那裏你可以伊斯利得到哪些特定annotaion的按鈕被竊聽

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control 
{ 
annotation *annView = view.annotation; 
detailedViewOfList *detailView = [[detailedViewOfList alloc]init]; 
detailView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
detailView.address = annView.address; 
detailView.phoneNumber = annView.phonenumber; 
[self presentModalViewController:detailView animated:YES]; 
} 

這裏annotaion是一類進口MKAnnotaion.h和地址和PhoneNumber是annotaion類的屬性可以讓更多的同時的DetailView類的地址和PhoneNumber性都很強。這樣你就可以傳遞值。希望對你有幫助!

+0

Thnx詳細答案我有一個問題:哪裏以及我如何填寫或設置annView.address? – ercan

+0

你如何在地圖上獲得註釋? – superGokuN

+0

在我的viewdidload方法 for(int i = 0; i <[arrLat count]; i ++) {CLLocationCoordinate2D theCoordinate1; konumpin * myAnnotation1 = [[konumpin alloc] init]; theCoordinate1.latitude = [[arrLong objectAtIndex:i] doubleValue]; theCoordinate1.longitude = [[arrLat objectAtIndex:i] doubleValue]; myAnnotation1.coordinate = theCoordinate1; myAnnotation1。title = [arrMagaza objectAtIndex:i]; [annotations addObject:myAnnotation1]; } [mtMap addAnnotations:annotations]; – ercan

0

婁是MapView委託方法......

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 

} 

當用戶敲擊註釋銷上述方法調用automaticaly如果方法在.m文件螺母限定第一聲明MKMapViewDelegate代表在h文件

,你也可以從谷歌的地方API獲得標題或副標題和id .... 鏈接... https://developers.google.com/maps/documentation/places/

+0

感謝您的回答。我很新的ios所以請你能告訴我如何設置detail.address例如有沒有任何示例應用程序或此標註方法的代碼 – ercan

+0

確定老兄,http://mithin.in/2009/06/22/使用iphone-sdk-mapkit-framework-a-tutorial這個鏈接只返回這個鏈接上的子頁面和標題,但你可以使用上面的代理方法insted查看標題和副標題... –

0
for(int i=0; i<[arrLat count];i++) { 

CLLocationCoordinate2D theCoordinate1; 
konumpin* myAnnotation1=[[konumpin alloc] init]; 

theCoordinate1.latitude = [[arrLong objectAtIndex:i] doubleValue]; 

theCoordinate1.longitude = [[arrLat objectAtIndex:i] doubleValue]; 

myAnnotation1.coordinate=theCoordinate1; 
myAnnotation1.title=[arrMagaza objectAtIndex:i]; 
[annotations addObject:myAnnotation1]; 

} 

[mtMap addAnnotations:annotations]; 
相關問題