0

我正在尝试遍历数组中的地址,对它们进行地理编码并作为注释添加到 MKMapview。这是我遇到的崩溃:[LocationAnnotation 坐标]:无法识别的选择器发送到实例 0x205ce5c0 这是我的代码:

#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]);

}
4

1 回答 1

1

您的 LocationAnnotation 类没有实现-coordinate方法。您已经创建了实例变量,但没有创建返回其值的方法:

- (CLLocationCoordinate2D)coordinate {
    return coordinate;
}
于 2013-01-04T19:50:19.770 回答