Search code examples
iosobjective-ccllocationmanagerskmaps

SKMap not showing because of SKCoordinateRegion


I have a strange issue that started happening recently, and i cannot figure out why.

When I initialise my map with this piece of code, everything works perfectly:

_mapView = [[SKMapView alloc] initWithFrame:CGRectMake( 0.0f, 0.0f,  CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) )];

[self.view insertSubview:_mapView atIndex:0];

_mapView.settings.rotationEnabled = NO;
_mapView.settings.displayMode = SKMapDisplayMode2D;

[SKRoutingService sharedInstance].mapView = _mapView;

But, when I want to zoom in to a specific region on the map, things go funky and the screen is just blue.

Code:

[self.locationManager startUpdatingLocation];
_lattitude = self.locationManager.location.coordinate.latitude;
_longitude = self.locationManager.location.coordinate.longitude;
SKCoordinateRegion region;
region.center = CLLocationCoordinate2DMake(_latitude, _longitude);
region.zoomLevel = 14;
_mapView.visibleRegion = region;
[self.locationManager stopUpdatingLocation];

Screenshot:

enter image description here

Now, I'd like to mention that the blue screen only appears on the first launch. If i close the app and start it again, the map will be displayed properly (Both in Emulator and on the real device).

I tracked which piece of code is causing this, and it's this one:

_mapView.visibleRegion = region;

How can I fix this?


Solution

  • Probably the location manager has not yet received its position and gives you 0,0 which is somewhere in front of Africas coast. Ocean = blue.

    You will need to setup a delegate for the location manager and wait for a positive fix on your position and then display the region in your map. In your code above you immediately request the location after starting the location manager.