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 c开发者_高级运维oordinates 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
That'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.
精彩评论