objective-c - CoreLocation 对象,无法分配经度

标签 objective-c ios

我正在构建一个与 MapKit 配合使用的应用,我需要从 Google map 获取坐标。

我创建了一个函数来执行此操作(通过调试,我知道我得到了好的坐标),但是当我尝试将其分配给事件对象的坐标时,它崩溃了。

下面是代码,我已经用 // <---------------- 明确注释了失败的地方...

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

事件.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

事件.m

#import "Event.h"

@implementation Event

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

@end

值得注意的是,也许 ARC 已开启。

最佳答案

您是否尝试过将坐标属性更改为读写? (分配将是一个不错的选择。)

关于objective-c - CoreLocation 对象,无法分配经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11472271/

相关文章:

objective-c - 如何在扩展中定义的 Objective-C 中使用静态方法

objective-c - 重用以编程方式创建的 UITableView?

objective-c - 即使不需要也使用弱变量的缺点?

objective-c - UISplitViewController 中的 PreferredPrimaryColumnWidthFraction 是否有限制?

ios - PFAnonymousUtils.logIn 出错

ios - .hash 给出 EXC_BREAKPOINT (code=EXC_ARM_BREAKBOINT

iphone - Objective-C程序收到信号: "EXC_BAD_ACCESS" push notifications

ios - 如何为 arm64 位支持 iOS 构建 libssh2

iphone - 使用 GData Obj-C 设置 YouTube 视频的隐私

ios - Flex 移动项目 : Open PDF in Native Reader on iOS