2

i'm wondering how i can make this code work : when i click on a cell, i call the DetailViewController, which is a map with the coordinates of San Francisco. But my map shows only the "world" map, and don't go to the region i set up, do you know how i could start off with "my" coordinates?

Here's my code :

#import "DetailViewController.h"

@implementation DetailViewController

@synthesize mapView;

- (void) gotoLocation {
    NSLog(@"allo");

    //start off in San Francisco
    MKCoordinateRegion region;
    region.center.latitude = 37.786996;
    region.center.longitude = -122.440100;
    region.span.latitudeDelta = 0.112872;
    region.span.longitudeDelta = 0.109863;

    [self.mapView setRegion:region animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self gotoLocation];
}

- (void)dealloc {
    [super dealloc];
    [mapView release];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    self.mapView = nil;
}

@end

Thanks

Paul

Paul
  • 6,108
  • 14
  • 72
  • 128

3 Answers3

8
- (void)viewDidLoad{
[super viewDidLoad];

// define span for map: how much area will be shown
MKCoordinateSpan span;
span.latitudeDelta = 0.002;
span.longitudeDelta = 0.002;

// define starting point for map
CLLocationCoordinate2D start;
start.latitude = 37.786996;
start.longitude = 0.109863;

// create region, consisting of span and location
MKCoordinateRegion region;
region.span = span;
region.center = start;

// move the map to our location
[self.mapView setRegion:region animated:YES];

}

rich
  • 2,136
  • 19
  • 23
  • Thanks rich, i forgot to use IB with the mapView, thanks for the explanations anyway ! also, how can you know the latitude and longitude of a specific point in the world? i copied and paste the SF coordinates, but how would you know like "Rome" or "Buenos Aires" ? something with google maps...? – Paul Aug 01 '11 at 14:40
  • Use this website http://www.geonames.org/ to get the data for Rome, Buenos Aires, etc. or you could write some code to query it if you wanted to use it in your application – rich Aug 01 '11 at 14:59
  • hi rich, can you please tell me how you use the coordinate from the website you gave in your comment, for example : for Rio de Janeiro : lat : S 22° 54' 10'' , into this in Xcode : newRegion.center.latitude = 37.786996; ? Thanks a lot – Paul Aug 10 '11 at 14:45
  • This is exactly what you are looking for it will give you the float values MapKit takes http://itouchmap.com/latlong.html also can I get a vote for the help? – rich Aug 10 '11 at 21:58
  • yep, i've just registered, i could n't do it before, thanks a lot for the link ! – Paul Aug 10 '11 at 23:37
2

Since you see the world map, the map view's frame must be fine and that code to set the region should work.

The most likely reason the setRegion call doesn't work is that the mapView IBOutlet is not hooked up to the map view control in the xib. In IB, right-click on File's Owner and connect the mapView outlet to the map view control.

You may also want to connect the map view control's delegate outlet to File's Owner as well so any delegate methods you implement (later) will get called.

Separately (and unrelated to your problem), in dealloc, [super dealloc] should be called last. See this answer for an explanation.

Community
  • 1
  • 1
  • Thanks Anna for this great answer! i did not know about the super dealloc, thanks also for that. Alright it works perfectly! – Paul Aug 01 '11 at 14:38
1

Your frame needs to be set before you can set the region, by the looks of this you are using IB to instantiate the mapview, so you should probably put the setRegion message into the viewDidAppear: and put this into your DetailViewController.

-(void)viewDidAppear {
    MKCoordinateRegion region;
    region.center.latitude = 37.786996;
    region.center.longitude = -122.440100;
    region.span.latitudeDelta = 0.112872;
    region.span.longitudeDelta = 0.109863;
    [self.mapView setRegion:region animated:YES];
}

or

you can just do something like this before you set the region (may not work)

self.mapView.frame = self.view.bounds
bigkm
  • 2,218
  • 1
  • 16
  • 12
  • thanks for the answer, actually i forgot to link the mapView in IB. Thanks anyway ! – Paul Aug 01 '11 at 14:38