通过表视图和地图视图传递数据...它是如何工作的?

发布于 2024-12-13 21:17:29 字数 1695 浏览 6 评论 0原文

我的应用程序有一个带有两个视图的 TabBar。一个是地图,另一个是带有搜索字段的表格视图。在表视图控制器中,我通过 JSON 获取一些位置(带有坐标)。我需要知道如何检索这些信息以将注释放入地图中。

在表视图中:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MapViewController *mvc = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];

CLLocationCoordinate2D location;
titleLocation       = [ streets objectAtIndex:indexPath.row ];
location.latitude   = [ [ latitude objectAtIndex:indexPath.row ] doubleValue];
location.longitude  = [ [ longitude objectAtIndex:indexPath.row ] doubleValue];

[self.tabBarController setSelectedIndex:0];
[mvc release];

我知道它不起作用,但我不知道如何将此值传递给 MapViewController 或我的 MapViewAnnotation,然后放入 MapViewController 中。我认为最后一个比第一个更好。

你能帮我吗? 非常感谢


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MapViewController *mvc = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];

    CLLocationCoordinate2D location;
    titleLocation       = [ streets objectAtIndex:indexPath.row ];
    location.latitude   = [ [ latitude objectAtIndex:indexPath.row ] doubleValue];
    location.longitude  = [ [ longitude objectAtIndex:indexPath.row ] doubleValue];

    MapViewAnnotation *mva = [[MapViewAnnotation alloc] initWithTitle:titleLocation andSubtitle:@"Clique para fazer sua contribuição" andCoordinate:location];

    NSLog(@"Lat/Log: %f/%f", location.latitude, location.longitude);

    [mvc setAnnotation:mva];
    [mvc.mapView addAnnotation:mva]; 

    [self.tabBarController setSelectedIndex:0];
    [mvc release];  
}

我这样设置,但它仍然不起作用。注释不出现。

My app has a TabBar with two views. One is a map and other is a table view with search field. In table view controller I get some places (with coordinates) through JSON. I need to know how to retrieve these informations to put an Annotation in a map.

in TABLE VIEW:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MapViewController *mvc = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];

CLLocationCoordinate2D location;
titleLocation       = [ streets objectAtIndex:indexPath.row ];
location.latitude   = [ [ latitude objectAtIndex:indexPath.row ] doubleValue];
location.longitude  = [ [ longitude objectAtIndex:indexPath.row ] doubleValue];

[self.tabBarController setSelectedIndex:0];
[mvc release];

I know it will not work, but I don't know how to pass this values for MapViewController or my MapViewAnnotation then put into MapViewController. This last one I think is better then the first one.

Could you help me?
Thanks a lot


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MapViewController *mvc = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];

    CLLocationCoordinate2D location;
    titleLocation       = [ streets objectAtIndex:indexPath.row ];
    location.latitude   = [ [ latitude objectAtIndex:indexPath.row ] doubleValue];
    location.longitude  = [ [ longitude objectAtIndex:indexPath.row ] doubleValue];

    MapViewAnnotation *mva = [[MapViewAnnotation alloc] initWithTitle:titleLocation andSubtitle:@"Clique para fazer sua contribuição" andCoordinate:location];

    NSLog(@"Lat/Log: %f/%f", location.latitude, location.longitude);

    [mvc setAnnotation:mva];
    [mvc.mapView addAnnotation:mva]; 

    [self.tabBarController setSelectedIndex:0];
    [mvc release];  
}

I put like this but it still doesn't work. The Annotation doesn't appear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云胡 2024-12-20 21:17:29

假设地图始终位于选项卡索引 0 上,您可以使用选项卡栏控制器的 viewControllers 数组直接访问 MapViewController

MapViewController *mvc = (MapViewController *)
                     [self.tabBarController.viewControllers objectAtIndex:0];

CLLocationCoordinate2D location;
titleLocation       = [ streets objectAtIndex:indexPath.row ];
location.latitude   = [ [ latitude objectAtIndex:indexPath.row ] doubleValue];
location.longitude  = [ [ longitude objectAtIndex:indexPath.row ] doubleValue];

//Here, call some method in MapViewController that takes a location and title
//and creates an annotation from it and adds it to the map view...
[mvc makeAnnotationAtLocation:location title:titleLocation];

[self.tabBarController setSelectedIndex:0];

//(do not release mvc)

makeAnnotationAtLocation:title:方法可能如下所示:

- (void)makeAnnotationAtLocation:(CLLocationCoordinate2D)coord title:(NSString *)locTitle
{
    YourAnnotationClass *yac = [[YourAnnotationClass alloc] init];
    yac.coordinate = coord;
    yac.title = locTitle;
    [mapView addAnnotation:yac];
    [yac release];

    //center map on coord if you want to...
}

Assuming the map is always on tab index 0, you can access that MapViewController directly using the tab bar controller's viewControllers array:

MapViewController *mvc = (MapViewController *)
                     [self.tabBarController.viewControllers objectAtIndex:0];

CLLocationCoordinate2D location;
titleLocation       = [ streets objectAtIndex:indexPath.row ];
location.latitude   = [ [ latitude objectAtIndex:indexPath.row ] doubleValue];
location.longitude  = [ [ longitude objectAtIndex:indexPath.row ] doubleValue];

//Here, call some method in MapViewController that takes a location and title
//and creates an annotation from it and adds it to the map view...
[mvc makeAnnotationAtLocation:location title:titleLocation];

[self.tabBarController setSelectedIndex:0];

//(do not release mvc)

The makeAnnotationAtLocation:title: method might look like this:

- (void)makeAnnotationAtLocation:(CLLocationCoordinate2D)coord title:(NSString *)locTitle
{
    YourAnnotationClass *yac = [[YourAnnotationClass alloc] init];
    yac.coordinate = coord;
    yac.title = locTitle;
    [mapView addAnnotation:yac];
    [yac release];

    //center map on coord if you want to...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文