我有一个应用程序,其中列出了许多将纬度和经度存储在数据库中的场所。我用json填充列表。然后我在动态表格中显示结果。在牢房中,我的距离以公里为单位。我通过获取用户当前位置并使用场地坐标计算距离来找到此距离。
它工作正常但我的问题是视图加载,距离错误,直到用户位置更新3-5秒后,它显示正确的距离。
我获取用户位置并在viewdidload中填充单元格。
我的问题是:是否可以在上一个视图中获取用户的位置并将其传递到此视图,以便最初显示的距离正确?
以下是代码:
我首先在viewdidload中获取用户loc:
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
然后我得到我的json数据等等。然后我做tableView:
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [rows count];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// int degrees = newLocation.coordinate.latitude;
// double decimal = fabs(newLocation.coordinate.latitude - degrees);
// int minutes = decimal * 60;
// double seconds = decimal * 3600 - minutes * 60;
// NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
// degrees, minutes, seconds];
NSString *lat = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];
latLabel.text = lat;
// degrees = newLocation.coordinate.longitude;
// decimal = fabs(newLocation.coordinate.longitude - degrees);
// minutes = decimal * 60;
// seconds = decimal * 3600 - minutes * 60;
// NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
// degrees, minutes, seconds];
NSString *longt = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];
longLabel.text = longt;
//CGFloat Alat = [lat floatValue];
//CGFloat Alon = [longt floatValue];
self.aalat = lat;
self.aalon = longt;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell.
NSDictionary *dict = [rows objectAtIndex: indexPath.row];
THELATITUDE = [dict objectForKey:@"L_LAT"];
THELONGITUDE = [dict objectForKey:@"L_LON"];
CGFloat Nlat = [THELATITUDE floatValue];
CGFloat Nlon = [THELONGITUDE floatValue];
CGFloat NNlat = [aalat floatValue];
CGFloat NNlon = [aalon floatValue];
// NSLog(@"lat1: %f", Nlat);
// NSLog(@"Lon1: %f", Nlon);
// NSLog(@"lat2: %f", NNlat);
// NSLog(@"Lon2: %f", NNlon);
// NSLog(@"aalat: %@", aalat);
// NSLog(@"aalon: %@", aalon);
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:Nlat longitude:Nlon];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:NNlat longitude:NNlon];
// NSLog(@"Distance i meters: %f", [location1 getDistanceFrom:location2]);
float mesafe1 = [location1 getDistanceFrom:location2 ] /1000;
int x1 = mesafe1 * 100.0;
float mesafe2 = (float) x1 / 100.0;
//Create a string.
NSString *strOne = [[NSString alloc] init];
NSString *strTwo = [[NSString alloc] init];
NSString *strThree = [[NSString alloc] init];
NSString *strFour = [[NSString alloc] init];
NSString *strFive = [[NSString alloc] init];
//Append strings.
strOne = @"MESAFE: ";
strTwo = [NSString stringWithFormat:@"%0g", mesafe2];
strThree = @" kilometre";
strFour = [strOne stringByAppendingString:strTwo];
strFive = [strFour stringByAppendingString:strThree];
NSString *mesafe = strFive;
cell.textLabel.text = [dict objectForKey:@"L_NAME"];
//cell.detailTextLabel.text = [dict objectForKey:@"LISTING_ID"];
cell.detailTextLabel.text = mesafe;
[cell.textLabel setTextColor:[UIColor blackColor]];
return cell;
}
答案 0 :(得分:2)
使用NSUserDefault方法保存用户当前位置的纬度和经度,并将其用于您的视图。使位置更新管理器开始不停止,这样你就可以获得精确的纬度和经度
在用户位置更新时使用它
[[NSUserDefaults standardUserDefaults] setObject:latitude forKey:@"latitude"];
[[NSUserDefaults standardUserDefaults] setObject:latitude forKey:@"longitude"];
在计算距离的地方使用它
NSString *lat = [[NSUserDefaults standardUserDefaults] objectForKey:@"latitude"];
NSString *long = [[NSUserDefaults standardUserDefaults] objectForKey:@"longitude"];
答案 1 :(得分:0)
在上一个视图中获取用户的位置,然后在加载listview时将数据发送到这样的视图(在这种情况下userLocation可以是具有长和纬度的JSON字符串)
- (void) loadVenuesListView {
VenuesViewController *venuesView = [[VenuesViewController alloc] initWithNibName:@"VenuesViewController" bundle:nil];
venuesView.userLocation = fetchedUserLocation;
[[self navigationController] pushViewController:venuesView animated:YES];
[venuesView release];
}
然后在VenuesViewController中你必须创建一个属性:
@property (nonatomic, retain) NSString *userLocation;