UITableView中的“迷你地图”

时间:2011-05-15 20:41:00

标签: iphone ios ios4 mapkit

如何在UITableViewController中添加“迷你地图”?

我想使用与地图应用相同的“设计/布局”。请参阅:

enter image description here

3 个答案:

答案 0 :(得分:10)

您可以考虑使用Google Static Maps API,而不是在UI中使用功能齐全的MKMapView。您可以使用适当的位置格式化URL并异步获取图像。您可以将结果粘贴到UIImageView

http://maps.google.com/maps/api/staticmap?center=kakn%C3%A4stornet&size=88x88&maptype=roadmap&sensor=false

http://maps.google.com/maps/api/staticmap?center=kakn%C3%A4stornet&size=88x88&maptype=roadmap&sensor=false

只需确保缓存结果,这样每次有人运行您的应用时,您都不会访问Google服务器。

以下是包含marker的网址:

http://maps.google.com/maps/api/staticmap?center=kakn%C3%A4stornet&size=88x88&maptype=roadmap&markers=size:tiny|color:green|kakn%C3%A4stornet&sensor=false

enter image description here

答案 1 :(得分:4)

我想出来了,或者至少差不多。感谢@AhmadTK。

这是我的代码:

#import <MapKit/MapKit.h>
#import "RackAnnotation.h"

#import <QuartzCore/CATransaction.h>
#import <QuartzCore/CAAnimation.h>

[...]

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }

        [cell.textLabel setText:theAnnotationSet._address];

        [cell.textLabel setShadowColor:[UIColor whiteColor]];
        [cell.textLabel setShadowOffset:CGSizeMake(0, 1)];

        MKMapView *mapView=[[MKMapView alloc] initWithFrame:CGRectMake(1, 1, 68, 68)];
        mapView.mapType=MKMapTypeStandard;
        mapView.showsUserLocation=NO;
        [mapView setContentMode:UIViewContentModeBottomRight];

        MKCoordinateRegion region;
        region.center=theAnnotationSet._coordinate;

        MKCoordinateSpan span;
        span.latitudeDelta = 0.01;
        span.longitudeDelta = 0.01;
        region.span=span;

        [mapView setRegion:region animated:NO];
        [mapView addAnnotation:theAnnotationSet];
        [mapView setScrollEnabled:NO];
        [mapView setUserInteractionEnabled:NO];

        UIButton *mapButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
        [mapButton setBackgroundColor:[UIColor grayColor]];

        CALayer *mapLayer = mapView.layer;
        mapLayer.masksToBounds = YES;
        mapLayer.cornerRadius = 10.0;

        [mapButton addSubview:mapView];
        [mapButton setContentEdgeInsets:UIEdgeInsetsMake(1, 1, 1, 1)];

        CALayer *layer = mapButton.layer;
        layer.masksToBounds = YES;
        layer.cornerRadius = 10.0;

        [cell addSubview:mapButton];
        [cell setAccessoryView:mapButton];
        [cell setEditing:YES];

        NSLog(@"Title: %@", theAnnotationSet._address);

        return cell;

    }
    else {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }


        return cell;
    }
}

这就是结果:

enter image description here

我留下的唯一问题是在左侧。据我所知,只能通过使用您自己的子类和UITableViewCell或将您自己的视图设置为标题来完成。

我要弄清楚的另一件事是如何删除这个迷你地图中的“Googl”。我的意思是,我在主地图中有它,现在它只是让我讨厌。

干杯, 保罗佩伦

答案 2 :(得分:2)

我相信您在问题中的示例是通过UITableView header view实现的。尝试将UIView拖到您UITableView Controller,然后自定义UIView。它应该比修改TableViewCell容易得多。

参考:

UITableView Header (not section header) problem

相关问题