2013-01-04 38 views
0

我想循環訪問數組中的地址,對它們進行地理編碼並將其添加到MKMapview中作爲註釋。 這墜毀我得到:[LocationAnnotation座標]:無法識別的選擇發送到實例0x205ce5c0 這裏是我的代碼:在iOS6中向MKMapView添加MKAnnotation時崩潰

#import <UIKit/UIKit.h> 
#import "MapKit/MapKit.h" 
#import <CoreLocation/CoreLocation.h> 

@interface LocationAnnotation : NSObject<MKAnnotation> 
{ 
    CLLocationCoordinate2D coordinate; 
    NSString *mTitle; 
    NSString *mSubTitle; 
    NSInteger tag; 
} 
@property(nonatomic)NSInteger tag; 
@end 

    @interface RoadmapMerchantMapView : UIView<CLLocationManagerDelegate,MKMapViewDelegate> 
    { 
     MKMapView * mapView; 
     CLLocationManager * currentLocation; 
     CLLocationCoordinate2D fuelLocationCoordinate; 
     NSString * typeSearch; 
     NSArray * allStations; 
     LocationAnnotation * stationAnn; 
    } 


    - (id)initWithFrame:(CGRect)frame withData:(NSArray *)data; 
    @end 

#import "RoadmapMerchantMapView.h" 

@implementation LocationAnnotation 

-(NSString *) title 
{ 
    return mTitle; 
} 

-(NSString *) subtitle 
{ 
    return mSubTitle; 
} 

-(id)initWithCoordinate:(CLLocationCoordinate2D)Mycoordinate Title:(NSString *)title subTitle:(NSString *)subTitle annIndex:(int)index{ 
    mTitle = title; 
    mSubTitle = subTitle; 
    coordinate = Mycoordinate; 
    return self; 
} 

@end 

@implementation RoadmapMerchantMapView 

- (id)initWithFrame:(CGRect)frame withData:(NSArray *)data 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 

    } 

    [self layoutLocation:data]; 
    return self; 
} 

-(void)zoomToUserLocation:(CLLocationCoordinate2D)userLocation 
{ 
    MKCoordinateRegion region; 

    region.center = userLocation; 
    region.span = MKCoordinateSpanMake(3.0, 3.0); 
    region = [mapView regionThatFits:region]; 
    [mapView setRegion:region animated:YES]; 
} 


-(void)drawMap { 
    mapView = [[MKMapView alloc]initWithFrame:self.bounds]; 
    mapView.mapType = MKMapTypeStandard; 
    mapView.delegate = self; 
    [self addSubview:mapView]; 
} 

-(void)layoutLocation:(NSArray*)items 
{  
    [self drawMap]; 

    allStations = [[NSArray alloc]initWithArray:items]; 
    if ([allStations count]>0) { 
     for (int i=0; i<1; i++) { 
      NSDictionary * itemNo = [items objectAtIndex:i]; 

      NSString * fullAddress = [NSString stringWithFormat:@"%@,%@,%@,%@",[itemNo objectForKey:@"address"],[itemNo objectForKey:@"city"],[itemNo objectForKey:@"state"],[itemNo objectForKey:@"zip"]]; 

      CLGeocoder * geoCoder = [[CLGeocoder alloc]init]; 
      [geoCoder geocodeAddressString:fullAddress completionHandler:^(NSArray *placemarks, NSError *error) { 

       if (error) { 
        NSLog(@"Geocode failed with error: %@", error); 
        return; 
       } 

       if(placemarks && placemarks.count > 0) 
       { 
        CLPlacemark *placemark = placemarks[0]; 
        CLLocation *location = placemark.location; 
        CLLocationCoordinate2D coords = location.coordinate; 
        NSLog(@"Latitude = %f, Longitude = %f", 
          coords.latitude, coords.longitude); 
        NSString * name = [itemNo objectForKey:@"name"]; 
        stationAnn = [[LocationAnnotation alloc]initWithCoordinate:coords Title:name subTitle:@"Offer" annIndex:i]; 
        //stationAnn.tag = i; 
        [mapView addAnnotation:stationAnn]; 

       } 
      }]; 
     } 

    } 
    else { 

     UIAlertView * av = [[UIAlertView alloc]initWithTitle:@"Not Found" message:@"There are no station in your area. Select state to get more results." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [av show]; 
     } 

} 


- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    static NSString * stationLoc = @"stationIdentifier"; 
    MKPinAnnotationView * customPinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:stationLoc]; 
    customPinView.pinColor = MKPinAnnotationColorGreen; 
    customPinView.animatesDrop = YES; 
    customPinView.canShowCallout = YES; 

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    NSUInteger index = [(LocationAnnotation *)annotation tag]; 
    rightButton.tag = index; 
    [rightButton addTarget:self 
        action:@selector(showDetails:) 
      forControlEvents:UIControlEventTouchUpInside]; 
    customPinView.rightCalloutAccessoryView = rightButton; 

    return customPinView; 
} 

-(void) showDetails:(id)sender { 
    NSLog(@"Tag:%d",[sender tag]); 

} 

回答

1

你LocationAnnotation類不實現-coordinate方法。你已經創建了實例變量,但不是返回其值的方法:

- (CLLocationCoordinate2D)coordinate { 
    return coordinate; 
} 
+0

嗯..我想知道爲什麼它在那之前工作。讓我試試這個 – Ashutosh

+0

它工作。非常感謝 – Ashutosh