2010-08-26 81 views
3

銷我使用iPhone核心位置:區分自定義圖釘圖像

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

畫我自己的形象自定義針。我想爲不同的針腳使用不同的圖像。我想知道如何區分哪個引腳正在調用此函數。

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

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyPin"]; 
    annView.canShowCallout = YES; 

    annView.calloutOffset = CGPointMake(-5, 5); 

    if ([annView.annotation.title isEqualToString:myLocation]) { 
     UIImage *pinImage = [UIImage imageNamed:@"myLocationImage.png"]; 
     [annView setImage:pinImage]; 
    } else { 
     UIImage *pinImage = [UIImage imageNamed:@"resImage.png"]; 
     [annView setImage:pinImage]; 
    } 

    return annView; 
} 

編輯:重溫這個新項目,我意識到創建不同的針類是浪費的。更好的實現方法是設置引腳類型,然後將其從MKAnnotationView的註釋中讀出。下面的例子。

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

    MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PinID"]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [button addTarget:self action:@selector(viewStoreDetails:) forControlEvents:UIControlEventTouchDown]; 

    pin.rightCalloutAccessoryView = button; 
    pin.canShowCallout = YES; 
    pin.calloutOffset = CGPointMake(-5, 5); 

    Annotation *a = (Annotation *)pin.annotation; 

    int i = a.locationType; 

    switch (i) { 
     case RETAIL: 
      pin.image = [UIImage imageNamed:@"pin_retail.png"]; 
      break; 
     case OUTLET: 
      pin.image = [UIImage imageNamed:@"pin_outlet.png"]; 
      break; 
     case COMING_SOON: 
      pin.image = [UIImage imageNamed:@"pin_coming_soon.png"]; 
      break; 
     case MY_LOCATION: 
      pin.image = [UIImage imageNamed:@"pin_my_location.png"]; 
      break; 
      pin.image = [UIImage imageNamed:@"pin_retail.png"]; 
     default: 
      break; 
    } 

    return [pin autorelease]; 
} 

回答

3

annView.annotation.title取得了訣竅。

相關問題