在didSelectRowAtIndexPath中获取用户位置坐标(用于TableView单元格点击)

时间:2011-02-15 21:22:38

标签: iphone ios uitableview location cllocationmanager

这是代码 - 它本身就说明了(具体来说,参见代码中的@todo):

// Handle taps of certain table rows
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Set up start location
    CLLocationCoordinate2D start;

    // Initialize the location manager, to get user location
    locationController = [[JJGCLController alloc] init];
    locationController.delegate = self;
    [locationController.locationManager startUpdatingLocation];

    // @todo - Get the latitude/longitude values back from my controller, and
    // set them as the start coordinates.
    //start.latitude = location.coordinate.latitude;
    //start.longitude = location.coordinate.latitude;

    CLLocationCoordinate2D destination;
    destination.latitude = (CLLocationDegrees)[pLatitude doubleValue];
    destination.longitude = (CLLocationDegrees)[pLongitude doubleValue];

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

基本上,我正在尝试设置位置管理器/控制器以获取用户的位置,以便我可以设置一个链接,该链接将在地图应用中打开,其中包含从用户位置到目的地的路线(纬度/经度)分别来自pLatitude和pLongitude。

我已经按照Hello There:一个核心位置教程来添加我自己的位置控制器类(JJGCLController),并且在我的视图控制器的- (void)locationUpdate:(CLLocation *)location方法中,我可以看到位置,但我无法获得我的表视图didSelectRowAtIndexPath方法中的坐标。

基本上,我的应用在每个表格单元格中显示一个地址,当用户点击地址时,我希望加载地图应用,然后显示从当前用户位置到地址的路线。

1 个答案:

答案 0 :(得分:3)

在致电-startUpdatingLocation和访问位置数据之间至少需要一段时间。

有两种方法可以实现这一点 - 您可以更早地调用startUpdatingLocation(例如,当应用程序启动时,或者出现此特定视图时),或者您可以延迟将用户发送到地图应用程序直到调用locationUpdate:之后,这可能会在不可预测的时间之后发生。这两种方式都不理想。