获取 didSelectRowAtIndexPath 内的用户位置坐标(用于 TableView Cell 点击)

发布于 2024-10-17 09:52:45 字数 1648 浏览 3 评论 0原文

这是代码 - 它不言自明(具体请参阅代码中的 @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 方法中,我可以查看位置,但我无法获取表视图的 didSelectRowAtIndexPath 方法中的坐标。

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

Here's the code - it speaks for itself (see, specifically, the @todo in the code):

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

Basically, I'm trying to set up a location manager/controller to get the user's location so I can set up a link that will open in the Maps app with a route from the user's location to the destination (lat/lon from pLatitude and pLongitude, respectively).

I've followed the Hello There: A Core Location Tutorial to add my own location controller class (JJGCLController), and in my view controller's - (void)locationUpdate:(CLLocation *)location method, I can see the location, but I can't get the coordinates inside my Table View's didSelectRowAtIndexPath method.

Basically, my app shows an address in each table cell, and when the user taps on the address, I want the Maps app to load, and then a route from the current user location to the address to appear.

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

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

发布评论

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

评论(1

明媚如初 2024-10-24 09:52:45

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

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

There needs to be at least a small amount of time between calling -startUpdatingLocation and having access to the location data.

There are two ways to achieve this - you can either call startUpdatingLocation much earlier (for example, when the app starts, or when this particular view appears), or you can delay sending the user to the maps application until after locationUpdate: is called, which may happen after an unpredictable amount of time. Neither way is ideal.

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