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

标签 iphone objective-c ios ipad userlocation

我有一个应用程序列出了一些地点,这些地点的纬度和经度存储在数据库中。我用 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/

相关文章:

iphone - 从 UIPageViewController 中移除 View Controller

ios - 如何存储 UIPickerView 的选定值并将其显示在不同 ViewController 的标签中?

ios - 如何从中间截断 View 标题

iPhone Web 应用程序输入字段在位置 :absolute container

iphone - NSCC Calendar to Date 返回 nil

ios - aps-environment 始终在发展

objective-c - Xcode 中的 EXC_BAD_ACCESS (code=1) 错误

ios - 我的 Apple MacBook 硬盘驱动器崩溃 - 配置文件丢失,有没有办法让我仍然可以更新我的应用程序?

ios - 收不到推送通知 - ios

iphone - 停止 performSelector : from being performed