核心位置返回 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]];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两个问题:
1)您正在执行一个没有任何用处的
NSNumber
装箱/拆箱循环2)Core Location 是异步的,因此您无法立即返回用户的位置,您必须使用委托方法来检索位置(如果/当位置可用时)。
以下是它应该如何工作的示例:
Two issues:
1) You are doing a
NSNumber
box/unbox cycle that has no use whatsoever2) 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:
代码的第四行是问题开始的地方。您无法立即获取位置,您需要等待位置服务通知代理人位置可用。我将在您的代码中添加一些注释来解释:
由于您将委托设置为“self”,所以当前类但实现“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:
Since you set the delegate as "self", the current class but implement the "CLLocationManagerDelegate" protocol, which defines the following two functions:
That is where you will get a location provided to you.
Dont forget to update your class definition like so:
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: