2012-07-13 20 views
0

我正在構建一個與MapKit一起使用的應用程序,我需要從Google Maps獲取座標。CoreLocation對象,無法分配經度

我做了一個這樣做的功能(並且通過調試我知道我得到了很好的座標),但是當我嘗試將它分配給我的事件對象的座標時,它會崩潰。

下面是代碼,我已經清楚地// <----------------它無法評論...

AppDelegate.h

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

@interface AppDelegate : UIResponder <UIApplicationDelegate, MKMapViewDelegate, CLLocationManagerDelegate>{ 
    CLLocationManager *locationManager; 
} 

//-(void)makeTestData:(int)amount; 
-(void) addressLocation:(NSString*)adres:(Event*)event; 

@property (strong, nonatomic) UIWindow *window; 
//@property (strong, nonatomic) NSMutableArray *events; 


@end 

AppDelegate.m

#import "AppDelegate.h" 
#import "ListViewController.h" 
#import "Event.h" 
#import <RestKit/RestKit.h> 


@implementation AppDelegate{ 
    NSMutableArray *events; 

    /* for test data */ 
    NSMutableArray *titles; 
} 


@synthesize window = _window; 
//@synthesize events; 

-(void) addressLocation:(NSString*)adres: (Event*)event { 
    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv&key=MYKEY", 
          [adres stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]]; 
    NSArray *listItems = [locationString componentsSeparatedByString:@","]; 

    double latitude = 0.0; 
    double longitude = 0.0; 

    if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) { 
     latitude = [[listItems objectAtIndex:2] doubleValue]; 
     longitude = [[listItems objectAtIndex:3] doubleValue]; 
    } 
    else { 
     //Show error 
    } 
    CLLocationCoordinate2D location; 
    location.latitude = latitude; 
    location.longitude = longitude; // <----- Checked in debugging, coords are good 

    event.coordinate.latitude = latitude; // <----- can't assign the latitude here =/ 
    event.coordinate.longitude = longitude; // <----- can't assign the longitude here =/ 

} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 


    // START COMMUNICATOIN WITH REST INTERFACE 
    //RKClient* client = [RKClient clientWithBaseURL:@"link"]; 
    //NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]); 
    // -------------------TEST OBJECTS 
    //makeTestData(3); 

    events = [NSMutableArray arrayWithCapacity:3]; 
    titles = [NSMutableArray arrayWithCapacity:3]; 
    [titles addObject:@"Gent Jazz Festival"]; 
    [titles addObject:@"Over blauw bloed en witte billenbijters - zoektocht in het Prinsenhof (zonder gids)"]; 
    [titles addObject:@"DuveltTent"]; 

    Event *event = [[Event alloc] init]; 
    event.title = [titles objectAtIndex:(rand()*100)%3]; 
    event.description = @"omminommie!!!"; 
    event.zone = @"fzfzefez"; 
    event.gemeente = @"Gent"; 
    event.plaats = @""; 
    event.straat = @"Sint-Veerleplein"; 
    NSString *adres = [NSString stringWithFormat:@"%@, %@", event.gemeente, event.straat]; 
    [self addressLocation:adres:event]; //<---- This function fails, see above for def 
    event.zone = @"blub";     
    [events addObject:event];    
... 

----編輯
像代碼一樣,如上所述,它甚至不編譯並給出錯誤:

Expression is not assignable

當我使用= location如上述建議我得到以下錯誤(我之前所做的那樣):

2012-07-13 17:14:20.608 Zone-It-new[565:707] -[Event setCoordinate:]: unrecognized selector sent to instance 0x283ab0 
2012-07-13 17:14:20.621 Zone-It-new[565:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Event setCoordinate:]: unrecognized selector sent to instance 0x283ab0' 
*** First throw call stack: 
(0x3748488f 0x35189259 0x37487a9b 0x37486a83 0x373e1650 0x6a319 0x6a5c9 0x31190cab 0x3118a7dd 0x31158ac3 0x31158567 0x31157f3b 0x33b9a22b 0x37458523 0x374584c5 0x37457313 0x373da4a5 0x373da36d 0x3118986b 0x31186cd5 0x6a097 0x6a03c) 
terminate called throwing an exception 

這裏是我的event.h和event.m

event.h

#import <Foundation/Foundation.h> 
#import <CoreLocation/CoreLocation.h> 
#import <MapKit/MapKit.h> 
#import <RestKit/RestKit.h> 

@interface Event : NSObject <MKAnnotation> 


// required property from mkanotation 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

// Optional 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *description; 
@property (nonatomic, copy) NSString *zone; 
@property (nonatomic, assign) NSString *gemeente; 
@property (nonatomic, copy) NSString *straat; 
@property (nonatomic, assign) int deelnemers; 
@property (nonatomic, copy) NSDate *start; 
@property (nonatomic, copy) NSDate *einde; 
@property (nonatomic, copy) NSString *plaats; 
@property (nonatomic, readonly) int id_nr; 
@property (nonatomic, assign) int huisnummer; 

@end 

event.m

#import "Event.h" 

@implementation Event 

@synthesize coordinate, title, zone, gemeente, straat, deelnemers, start, einde, plaats, id_nr, huisnummer, description; 

@end 

重要的是要注意,可能是ARC已打開。

+0

它告訴你,event.coordinate.latitude(和經度)是隻讀嗎?如果是這樣,你有沒有看過如果.latitude和.longitude是隻讀?也許嘗試使用[event.coordinate setLatitude:...]和[event.coordinate setLongitude:...] – 2012-07-13 14:25:32

+1

你嘗試過'event.coordinate = location'嗎? – 2012-07-13 14:28:32

+1

你會得到什麼錯誤? – Dustin 2012-07-13 14:28:37

回答

1

您是否嘗試將coordinate屬性更改爲readwrite? (assign將是一個不錯的選擇。)

+0

Tim,您也可以省略'readwrite',因爲它是默認值。 希望你也不知道這個:)。 '@property(nonatomic)CLLocationCoordinate2D coordinate;' – 2012-07-13 15:39:39

+0

我知道那麼多:) – Tim 2012-07-13 15:40:54