开发者

Updating the centre point & zoom of MKMapView (iPhone)

开发者 https://www.devze.com 2023-04-08 00:38 出处:网络
I have been trying to centre the map view on the users location. This should also update as and when a change in locat开发者_如何学编程ion is registered. The issue I\'m having is upon loading the app

I have been trying to centre the map view on the users location. This should also update as and when a change in locat开发者_如何学编程ion is registered. The issue I'm having is upon loading the app I can't get hold of the current latitude and longitude to apply the centring & zooming.

These are the key bits of code I have so far...

- (void)viewDidLoad
{
    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];

    [self updateMapZoomLocation:locationManager.location];

    [super viewDidLoad];
}

The idea being that it should create an instance of the location manager, then start updating the location. Finally it should run the function I wrote to update the map view accordingly...

- (void)updateMapZoomLocation:(CLLocation *)newLocation
{
    MKCoordinateRegion region;
    region.center.latitude = newLocation.coordinate.latitude;
    region.center.longitude = newLocation.coordinate.longitude;
    region.span.latitudeDelta = 0.1;
    region.span.longitudeDelta = 0.1;
    [map setRegion:region animated:YES];   
}

However this doesn't seem to happen. The app builds and runs ok, but all that gets displayed is a black screen - it's as if the coordinates don't exist?!

I also have a delegate method that deals with any updates by calling the function above...

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
           fromLocation:(CLLocation *)oldLocation
{
    [self updateMapZoomLocation:newLocation];
}

I have tried looking at several of the other similar questions that have been asked & answered previously however I still can't seem to find the solution I'm after.

Any help on this would be really appreciated; I've spent hours and hours trawling the internet in search of help and solutions.

ps. 'map' is the mapView.


You want to use the MKMapViewDelegate callback so you only zoom in once the location is actually known:

Here's some swift code to handle that:

var alreadyZoomedIn = false

func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
    if(!alreadyZoomedIn) {
        self.zoomInOnCurrentLocation()
        alreadyZoomedIn = true
    }
}

func zoomInOnCurrentLocation() {

        var userCoordinate = mapView?.userLocation.coordinate
        var longitudeDeltaDegrees : CLLocationDegrees = 0.03
        var latitudeDeltaDegrees : CLLocationDegrees = 0.03
        var userSpan = MKCoordinateSpanMake(latitudeDeltaDegrees, longitudeDeltaDegrees)
        var userRegion = MKCoordinateRegionMake(userCoordinate!, userSpan)

        mapView?.setRegion(userRegion, animated: true)
}


You shouldn't call updateMapZoomLocation during your viewDidLoad function because the location manager has not yet go a location. If/when it does it'll call the delegate function when it's ready. Until then your map won't know where to center. You could try zooming as far out as it goes, or remembering where it was last looking before the app was shutdown.

0

精彩评论

暂无评论...
验证码 换一张
取 消