I am showing a map view on front page of my application, I set the region and coords from current location using CLLocation.
When the application is run, a blue screen is being displayed on the mapview, when I open its child views and come back to main screen, the map show perfectly with location and zoom level.
Is there any thing wrong in the following code which causes the blue screen?
- (void)viewWillAppear:(BOOL)animated {
CLLocationManager *lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = kCLDistanceFilterNone;
[lm startUpdatingLocation];
CLLocation *location = [lm location];
CLLocationCoordinate2D coord;
coord = [location coordinate];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = coord.latitude; //39.281516;
zoomLocation.longitude= coord.longitude; //-76.580806;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];
[self.mainMapView setRegion:adjustedRegion animated:YES];
}
My guess would be that you are seeing blue because the location is (0, 0) which is in the middle of the sea. Probably because the first time round, the location on the
CLLocationManageris not set yet.You should probably check if there’s a location yet and if not then wait for a callback in the
CLLocationManagerDelegatemethodlocationManager:didUpdateToLocation:fromLocation:and then centre the map on the location you want.Something like this:
But also, you might want to take a look at
showsUserLocationofMKMapView.