核心位置返回 00

发布于 2024-11-07 02:58:20 字数 1422 浏览 1 评论 0 原文

这是完整的方法,我最初没有放置后半部分,因为我知道它有效:

 -(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]];

}

Here's the full method, I didn't put the second half originally because I know it works:

 -(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]];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

且行且努力 2024-11-14 02:58:20

两个问题:

1)您正在执行一个没有任何用处的 NSNumber 装箱/拆箱循环

2)Core Location 是异步的,因此您无法立即返回用户的位置,您必须使用委托方法来检索位置(如果/当位置可用时)。

以下是它应该如何工作的示例:

 -(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]];
}

Two issues:

1) You are doing a NSNumber box/unbox cycle that has no use whatsoever

2) Core Location is asynchronous, therefore you cannot return the user's location straight away, you must use the delegate method(s) to retrieve the location if/when it is available.

Here's an example of how it should work:

 -(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]];
}
我为君王 2024-11-14 02:58:20

代码的第四行是问题开始的地方。您无法立即获取位置,您需要等待位置服务通知代理人位置可用。我将在您的代码中添加一些注释来解释:

-(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 ...
}

由于您将委托设置为“self”,所以当前类但实现“CLLocationManagerDelegate”协议,定义了以下两个函数:

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

您将在那里获得为您提供的位置。

不要忘记像这样更新你的类定义:

@interface MyClass : NSObject <CLLocationManagerDelegate> {

顺便说一句,你似乎没有完全理解委托及其服务的目的,如果你是 Objective-C 的新手,但越来越熟悉它们,这很好。肯定会让您的生活更轻松一些。

其他一些有用的东西:

The fourth line of your code is where the problem begins. You cannot get the location instantly, you will need to wait until the location services notify the delegate that a location is available. I will add some comments to your code to explain:

-(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 ...
}

Since you set the delegate as "self", the current class but implement the "CLLocationManagerDelegate" protocol, which defines the following two functions:

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

That is where you will get a location provided to you.

Dont forget to update your class definition like so:

@interface MyClass : NSObject <CLLocationManagerDelegate> {

And as a side note, it seems like you do not fully understand delegates and the purpose that they serve, which is fine if you are new to Objective-C, but getting more familiar with them will certainly make your life a bit easier.

Some other helpful stuff:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文