Pradeep Mittal
Pradeep Mittal

Reputation: 593

mkmapview not showing according to the frame in nib file

Here is the screen of my nib file and map view but the map is always taking the whole view.I tried resetting my simulator and cleaning my Xcode but nothing works.Am i doing something wrong? Do i need to give mapview a frame programmatically?

http://prntscr.com/1sy1bw

I also want the button to be on top of mapview if the map cannot be set to take a particular frame.

Consider the below code please:

#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];

    }
}];
}

Upvotes: 1

Views: 284

Answers (1)

Pradeep Mittal
Pradeep Mittal

Reputation: 593

- (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];
        }
    }];
}

I commented the mapview alloc codeline and this fixed the issue.Seems like mapview gets pushed as soon as its allocated memory space.Moreover the frame coordinated that i initialised mapviewview with are still preserved although the code is commented.Dont know if it should work this way only or not but it did solved my issue .

Upvotes: 1

Related Questions