2011-05-13 175 views
0

下面是完整的方法,我没有把下半年原本因为我知道它的工作原理:核心位置返回00

-(void)showDirections 
{ 
    locationManager = [[CLLocationManager alloc] init]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; 
    [locationManager setDelegate:self]; 

    CLLocation *location = [locationManager location]; 

    CLLocationCoordinate2D coordinate = [location coordinate]; 
    NSNumber *myLatitude = [NSNumber numberWithDouble:coordinate.latitude]; 
    NSNumber *myLongitude = [NSNumber numberWithDouble:coordinate.longitude]; 
    double myLatitude2 = [myLatitude doubleValue]; 
    double myLongitude2 = [myLongitude doubleValue]; 


    NSArray *array = [dataHold objectForKey:@"Subtree"]; 
    NSString *latitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:4]]; 
    NSString *longitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:5]]; 
    double clubLatitude = [latitude doubleValue]; 
    double clubLongitude = [longitude doubleValue]; 
    CLLocationCoordinate2D start = { myLatitude2, myLongitude2};  
    CLLocationCoordinate2D destination = { clubLatitude, clubLongitude};   
    NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",start.latitude, start.longitude, destination.latitude, destination.longitude]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]]; 

} 
+3

好了,为什么在世界上你装箱/拆箱呢?看起来你可能需要更新一下'NSNumber'的位置: -/ – 2011-05-13 02:32:45

回答

1

两个问题:

1)你正在做一个NSNumber盒/ unbox循环,没有任何用处

2)核心位置是异步的,因此您不能立即返回用户的位置,您必须使用委托方法检索位置是否/何时可用。

下面是一个例子,说明它应该工作:

-(void)showDirections 
{ 
    locationManager = [[CLLocationManager alloc] init]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; 
    [locationManager setDelegate:self]; 

    [locationManager startUpdatingLocation]; 
} 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    CLLocationCoordinate2D coord = [newLocation coordinate]; 
    NSArray *array = [dataHold objectForKey:@"Subtree"]; 
    NSString *latitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:4]]; 
    NSString *longitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:5]]; 
    double clubLatitude = [latitude doubleValue]; 
    double clubLongitude = [longitude doubleValue]; 
    NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", coord.latitude, coord.longitude, clubLatitude, clubLongitude]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]]; 
} 
+0

我更新了它,你可以再看一次吗? – Eric 2011-05-13 02:40:44

+0

为什么在Locations苹果示例代码中这么简单? – Eric 2011-05-13 02:54:33

+0

CLLocationCoordinate2D坐标= [位置坐标]; // NSLog(@“%@”,坐标); \t [event setLatitude:[NSNumber numberWithDouble:coordinate.latitude]]; \t [event setLongitude:[NSNumber numberWithDouble:coordinate.longitude]]; – Eric 2011-05-13 02:54:40

1

代码的第四行是问题的开始位置。您无法立即获取该位置,您需要等到位置服务通知代理人有位置可用。我会添加一些评论你的代码解释:

-(void)showDirections 
{ 
    locationManager = [[CLLocationManager alloc] init]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; 
    [locationManager setDelegate:self]; 

    // this is not where you should be looking 
    CLLocation *location = [locationManager location]; 

    // the rest of this function is not included in this example ... 
} 

因为你设置的委托作为“自我”,当前的类,但实施“CLLocationManagerDelegate”协议,它定义了以下两个功能:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { /* */ } 
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { /* */ } 

这就是您将获得提供给您的位置的地方。

不要忘记更新您的类定义如下所示:

@interface MyClass : NSObject <CLLocationManagerDelegate> { 

而作为一个侧面说明,好像你不完全了解代表和他们所服务的宗旨,如果您是新这是罚款到Objective-C,但更熟悉它们一定会让你的生活变得更轻松。

其他一些有用的东西:

+0

的帮助,当我重新进入程序时,加载视图会将它发送回地图应用程序。我陷入了无限循环。 – Eric 2011-05-13 03:21:47

+0

这是一个设计决定,与核心位置无关。在UIApplicationDelegate中,你可以使用“application:didFinishLaunchingWithOptions:”来查看应用程序是否是通过URL启动的,如果是,那么就不要再启动它了:) – Nippysaurus 2011-05-13 03:29:56

+0

http ://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference。html#// apple_ref/occ/intfm/UIApplicationDelegate/application:didFinishLaunchingWithOptions: – Nippysaurus 2011-05-13 03:30:25