1

这是我的 nib 文件和地图视图的屏幕,但地图总是占据整个视图。我尝试重置我的模拟器并清理我的 Xcode,但没有任何效果。我做错了什么吗?我需要以编程方式给 mapview 一个框架吗?

http://prntscr.com/1sy1bw

如果无法将地图设置为采用特定框架,我还希望该按钮位于地图视图的顶部。

请考虑以下代码:

#import "mapViewController.h"

@interface mapViewController ()

@end

@implementation mapViewController
@synthesize mapView,source,dest,latdest,latsource,longdest,longsource;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
    dest=@"delhi";
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,0,160,240)];
CLGeocoder *geocoder1 = [[CLGeocoder alloc] init];
[geocoder1 geocodeAddressString:source
             completionHandler:^(NSArray* placemarks, NSError* error)
{
    for (CLPlacemark* aPlacemark in placemarks)
    {
        coordinate.latitude = aPlacemark.location.coordinate.latitude;
        latsource=&coordinate.latitude;
        coordinate.longitude = aPlacemark.location.coordinate.longitude;
        longsource=&coordinate.longitude;
        MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
        [annotation setCoordinate:(coordinate)];
        [annotation setTitle:source];
        annotation.subtitle = @"I'm here!!!";
        mapView.delegate = self;
        [self.mapView addAnnotation:annotation];

    }
}];
}
4

1 回答 1

1
- (void)viewDidLoad
{
[super viewDidLoad];
//mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,0,160,240)];
CLGeocoder *geocoder1 = [[CLGeocoder alloc] init];
[geocoder1 geocodeAddressString:source
             completionHandler:^(NSArray* placemarks, NSError* error)
    {
    for (CLPlacemark* aPlacemark in placemarks)
        {
        coordinate.latitude = aPlacemark.location.coordinate.latitude;
        latsource=&coordinate.latitude;
        coordinate.longitude = aPlacemark.location.coordinate.longitude;
        longsource=&coordinate.longitude;
        MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
        [annotation setCoordinate:(coordinate)];
        [annotation setTitle:source];
        annotation.subtitle = @"I'm here!!!";
        [self.view addSubview:mapView];
        [self.mapView addAnnotation:annotation];
        }
    }];
}

我评论了mapview alloc代码行,这解决了这个问题。似乎mapview在分配的内存空间后立即被推送。此外,尽管代码被注释,但我初始化mapviewview的框架仍然保留。不知道它是否应该工作唯一与否,但它确实解决了我的问题。

于 2013-09-23T06:21:08.203 回答