0
votes

I have an MKMapView, and I'm trying to set the map, so that when you first load the application it goes to a user set location, and if the user hasn't set one, to a default one. The problem is that it always seems to go to 0 latitude, 0 longitude.

-(void) viewDidLoad {

  [worldView setShowsUserLocation:YES];

  double longitude = [[NSUserDefaults standardUserDefaults] doubleForKey:WhereamiNewLongPrefKey];

  double latitude = [[NSUserDefaults standardUserDefaults] doubleForKey:WhereamiNewLatPrefKey];

  CLLocationCoordinate2D savedLoc = CLLocationCoordinate2DMake(latitude, longitude);

  MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(savedLoc, 250, 250);

  NSLog(@"latitude :%f", region.center.latitude);      
  NSLog(@"longitude :%f", region.center.longitude);

  [worldView setRegion:region animated:YES];
}

I've tried setting setShowUserLocation to NO, but that doesn't work. I know it's reading the correct region, because the NSLog is outputing the default latitude, but the map insists on going to somewhere in China...

I've also tried setting the region in mapView:didUpdateUserLocation:, but still the same result.

What am I doing wrong?

EDIT: I added the NSLog to output the longitude as well. The output is:

2012-11-14 09:50:30.699 Whereami[34256:13d03] latitude :20.516700
2012-11-14 09:50:30.699 Whereami[34256:13d03] longitude :99.900000
1
Change the NSLog to print out both latitude and longitude and tell us what it prints. It's possible that your longitude is wrong. Is it positive? That would mean it is in the eastern hemisphere.Craig
Are you testing with the Simulator or Device?rockstarberlin
@rockstarberlin I have tested in both, simulator, and the device.coopersita
@Craig I added the output to my original post.coopersita
Yup, positive longitude = eastern hemisphere, your code is fine, the data source is wrong.Craig

1 Answers

0
votes
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = 20.516700;
theCoordinate.longitude = 99.900000;

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(theCoordinate, 1000, 1000);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];  
[self.mapView setRegion:adjustedRegion animated:YES];