【问题标题】:Get User Location Coordinates inside didSelectRowAtIndexPath (for TableView Cell tap)在 didSelectRowAtIndexPath 中获取用户位置坐标(用于 TableView Cell 点击)
【发布时间】:2011-02-15 21:22:38
【问题描述】:

这是代码 - 它不言自明(具体参见代码中的@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: A Core Location Tutorial 添加我自己的位置控制器类 (JJGCLController),并且在我的视图控制器的 - (void)locationUpdate:(CLLocation *)location 方法中,我可以看到位置,但我无法获取坐标在我的 Table View 的 didSelectRowAtIndexPath 方法中。

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

【问题讨论】:

    标签: iphone ios uitableview location cllocationmanager


    【解决方案1】:

    在调用-startUpdatingLocation 和访问位置数据之间至少需要有一小段时间。

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

    【讨论】:

    • 我同意。您需要时间来获取位置。
    • 是否必须将用户转到 Maps.app?在您的应用程序中使用 MKMapView 是否可行?
    • @Mark - 我的应用程序中已经有一个 MapView 用于在地图上定位某些地方,但我不想添加任何东西来处理路线和导航,所以我很喜欢更快乐地将用户踢到地图上。另外,@grahamparks - 我决定在 viewDidLoad 中启动位置管理器,然后存储坐标以备后用。不理想,但它正在工作。我只是希望我能有我的蛋糕,也吃掉它。
    猜你喜欢
    • 2021-08-26
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多