0

緯度と経度がデータベースに保存されている多くの会場をリストするアプリがあります。リストに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;
}
4

2 に答える 2

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"];
于 2011-12-22T11:00:02.603 に答える
0

前のビューでユーザーの場所を取得し、リストビューをロードするときに、次のようにデータをビューに送信します (この場合の 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;
于 2011-12-22T11:01:38.203 に答える