Hi, I have a table view with a list of hotel, and i want put in cell.detailTextLabel.text the distance beetween userlocation and hotel. How can obtain the coordinates of userlocation? I see on web that i need to use CLLocationManager but i don't understand how and where implement in my table view. Then,to get the distance,i do a "getDistancefrom" between userLocation and the coordinates of the hotel ? Thanks
views:
99answers:
3That's really quite a big question ;)
This tutorial looks pretty good - I'd go through it and see if it helps. If not, come back to stackoverflow and ask more questions.
Well you have the right approach in mind, just need to clear out the details.
The simplest way is to have your table view controller adhere to the CLLocationManagerDelegate protocol. You then setup a location manager and have it start updating the user's location:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
Then you just implement this method from the delegate:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
In there you will be notified whenever a user's location changes. So then you can grab the newLocation longitude and latitude and compute the distance using all your hotels' longitudes and latitudes.
Make sure you import the core location framework to your project as I don't think it's there by default.
Good luck.
@deanWombourne I followed the tutorial you suggested; before to understand how calculate and display distance(it's a hard thing), i'm trying to display the coordinates of user location in cell.detailTextLabel.text but nothing appears in detailTextLabel. In the final step of tutorial there is this code
- (void)locationUpdate:(CLLocation *)location {
locationLabel.text = [location description];}
now, how do set the content of location label in cell.detailTextLabel? I tried also with string:
- (void)locationUpdate:(CLLocation *)location {
locationstring = [NSString stringwithFormat:@"%@",[location description];}
and then i set cell.detailTextLabel.text = locationstring;
but nothing appears!
@Nebs What you suggested is similar to tutorial suggested from deanWombourne!
I'm sorry but i'm a newbie with iphone/xcode world Thanks to both of you