如何在下一个屏幕上的地图上的表格中显示位置?

问题描述 投票:0回答:2

我正在将包含对象的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的新屏幕。但正如我所说,我真的不知道从屏幕上的表格到显示位置对象的地图和用户。

iphone objective-c mapkit cllocation
2个回答
0
投票

使用MapView创建一个新的ViewController,并将纬度和经度值传递给New ViewController。

假设您已将视图Controller创建为MyLocationMapViewController。

在SecondViewController.m中实现以下内容

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Location *location = [locationList objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:[[MyLocationMapViewController alloc] initWithLocation:location] animated:YES];
}

在我的位置图ViewController.h

  • (id)initWithLocation :( Location *)location;

在我的位置MapViewController.m

  • (id)initWithLocation :( Location *)location {* locationObj = location;

}

现在,您可以使用locationObj获取地图值并在MapView中显示坐标。


0
投票

在secondview控制器中创建MapViewController对象

在第二个视图控制器中写下面的代码

  • (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中创建2个名为latitude和longi的双变量来存储纬度和经度

© www.soinside.com 2019 - 2024. All rights reserved.