2015-05-11 40 views
0

我想在MKMapViewDelegate中使用一些委託方法。具體來說,我想給出彈出一個附件箭頭,以便當用戶觸摸它時,它會啓動本地地圖應用程序(用於全面映射功能)。IOS/Objective C/MKMapViewDelegate:獲取annotationView以顯示附件箭頭

我認爲我正確理解,一旦VC設置爲自己的委託,然後協議方法會自動調用。在那種情況下,委託方法是否會自動調用,或者您還需要做其他事情嗎?

這裏是啓動地圖的方法。

-(void) geoCodeAndMapIt { 
    NSString* location = @"156 University Ave, Palo Alto, CA 94301"; 
    CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
[geocoder geocodeAddressString:location 
      completionHandler:^(NSArray* placemarks, NSError* error){ 
       if (placemarks && placemarks.count > 0) { 
        CLPlacemark *topResult = [placemarks objectAtIndex:0]; 
        MKPlacemark *placemark = [[MKPlacemark alloc] 
               initWithPlacemark:topResult]; 
        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(placemark.coordinate, 5000, 5000);//5000 is meters 
        region.span.longitudeDelta /= 8.0; 
        region.span.latitudeDelta /= 8.0; 

        [self.mapView setRegion:region animated:YES]; 
        [self.mapView addAnnotation:placemark]; 
} 

下面是應該添加附件箭頭標註,但方法不點火:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"]; 
    annotationView.canShowCallout = YES; 
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    return annotationView; 
} 

我如何獲得這個方法來火,所以我得到附件箭頭?感謝您的任何建議。

+1

「一旦VC成立爲自己的代表」:不太準確。實際上,VC是作爲MKMapView的委託來設置的,我認爲它包含在VC中。您可以使用'mapView.delegate = self;'在代碼中執行此操作,也可以將地圖的代理插座連接到故事板中的VC。 – Anna

+0

這就是爲什麼我沒有得到配件視圖。當我在viewDidLoad中包含你的行時,我得到了附件。但是,我沒有得到箭頭......而是我得到了小信息按鈕。 IOS7中有這種變化嗎?或者我怎麼能得到箭頭? – user1904273

+1

關於UIButtonTypeDetailDisclosure的箭頭,請參閱http://stackoverflow.com/questions/22012468/mkannotationview-always-shows-infobutton-instead-of-detaildisclosure-btn。您必須使用自定義按鈕類型和自己的箭頭圖像。 – Anna

回答

3

爲什麼當你點擊註解視圖中的按鈕時不使用這個方法,這個方法被觸發,你可以在那裏執行你的自定義動作。

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { 
     //Do whatever you want to do here. 

     PropertyLocation *location = (PropertyLocation*)view.annotation; 

     for(PropertyDTO *property in self.items) 
     { 
      if(property.location == location) 
      { 
       PropertyDetailVC *detailVC = [PropertyDetailVC propertyDetailVCWithNodeID:property.nodeID]; 

       [self.navigationController pushViewController:detailVC animated:YES]; 
      } 
     } 

    }