gpt4 book ai didi

iPhone 应用程序在表格中显示 field 距离

转载 作者:行者123 更新时间:2023-11-28 23:09:42 26 4
gpt4 key购买 nike

我有一个应用程序列出了一些地点,这些地点的纬度和经度存储在数据库中。我用 json 填充列表。然后我在动态表中显示结果。在单元格中,我有 field 的公里距离。我通过获取用户当前位置并计算与 field 坐标的距离来找到这个距离。

它工作正常,但我的问题是在加载 View 时,距离是错误的,直到 3-5 秒后用户位置更新后,它才显示正确的距离。

我获取用户位置并填充 viewdidload 中的单元格。

我的问题是:是否可以获取用户在前一个 View 中的位置并将其传递到此 View ,以便最初显示的距离是正确的?

代码如下:

我先在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;
}

最佳答案

使用 NSUserDefault 方法保存用户当前位置的纬度和经度并将其用于您的 View 。让位置更新管理器开始而不是停止,这样你就可以得到准确的纬度和经度

在用户位置更新时使用它

     [[NSUserDefaults standardUserDefaults] setObject:latitude forKey:@"latitude"];
[[NSUserDefaults standardUserDefaults] setObject:latitude forKey:@"longitude"];

在计算距离的地方使用它

   NSString *lat = [[NSUserDefaults standardUserDefaults] objectForKey:@"latitude"];
NSString *long = [[NSUserDefaults standardUserDefaults] objectForKey:@"longitude"];

关于iPhone 应用程序在表格中显示 field 距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8602770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com