配列内の住所をループしてジオコーディングし、注釈として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]);
}