次の画面のマップ上にテーブルから場所を表示する方法は?
-
14-10-2019 - |
質問
オブジェクトをテーブルに表示する画面にオブジェクトを含むNSARRAYを渡しています。これらの各オブジェクトには、緯度プロパティと経度のプロパティが含まれています。ユーザーがセル(各セルがNSARRAYのオブジェクトを表す場所)を選択する機能を実装し、ユーザーが別の画面に持ち込まれ、マップ上のオブジェクトの位置を表す注釈が表示される可能性があります。ユーザーを表す注釈。どうすればいいですか? rootviewcontroller.mクラスの関連コードは次のとおりです。
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
self.secondViewController = controller;
[controller release];
self.secondViewController.locationList = sortedLocations;
[[self navigationController] pushViewController:controller animated:YES];
SecondViewController.mの私の関連コードは次のようになります。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"locationcell";
LocationTableViewCell *cell = (LocationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[LocationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
Location *location = [locationList objectAtIndex:indexPath.row];
cell.locationName.text = location.name;
cell.locationAddress.text = location.address;
cell.locationDistance.text = location.distance;
return cell;
}
目に見えるプロパティは名前、住所、距離であるが、位置オブジェクトには緯度と経度の特性も含まれていることに注意してください。 MapViewControllerという新しい画面を作成する必要があることを知っています。しかし、私が言ったように、私は画面のテーブルからどこに行くか、場所オブジェクトとユーザーを示す地図にどこに行くかわからない。
解決
MapViewを使用して新しいViewControllerを作成し、緯度と経度の価値を新しいViewControllerに渡します。
MyLocationMapViewControllerとしてView Controllerを作成したと仮定します。
SecondViewController.mで、以下を実装します
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Location *location = [locationList objectAtIndex:indexPath.row];
[self.navigationController pushViewController:[[MyLocationMapViewController alloc] initWithLocation:location] animated:YES];
}
mylocationmapviewcontroller.hで
- (id)initwithlocation :(場所 *)場所;
mylocationmapviewcontroller.mで
- (id)initWithlocation :(場所 *)場所{ *locationobj = location;
}
これで、Mapの値を取得するためにLocationOBJを使用して、MapViewに座標を表示できます。
他のヒント
SecondViewコントローラーでMapViewControllerオブジェクトを作成します
SecondViewコントローラーに以下のコードを記述します
(void)tableView :( uitableview *)tableView didselectrowatindexpath :( nsindexpath *)indexpath {
MapViewController *MapView = [[MapViewController alloc] initwithnibname:@"mapviewcontroller" bundle:[nsbundle mainbundle]];
self. mapView = controller; [mapView release]; self.mapView.latit = location.latitude; self.mapView.longi = location.longitude; [[self navigationController] pushViewController:controller animated:YES];
}
緯度と経度を保存するために、MapViewControllerでLATITとLONGIと呼ばれる2つの二重変数を作成します