开发者

Get User Location Coordinates inside didSelectRowAtIndexPath (for TableView Cell tap)

开发者 https://www.devze.com 2023-02-11 11:30 出处:网络
Here\'s the code - it speaks for itself (see, specifically, the @todo in the code): // Handle taps of certain table rows

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
    CLLocati开发者_如何学PythononCoordinate2D 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号