Question

So I am building a location based game and have a problem getting a users location to initialize properly. It doesn't always happen, but sometimes the location never changes from 0,0.

This causes a problem as I have a loading view (vault) that blocks the map from being displayed until the player's location is loaded and not 0,0. So if the user location doesnt get set, the vault never opens showing the map.

Is there any other way to ensure the user location is loaded? FYI - I already ensure they have location services enabled and their device is capable. I am having this issue on and off on my own devices with everything enabled properly.

ViewDidLoad:

/*Location Manager*/
locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self; 
[locationManager setDistanceFilter:100.0];//Update location to network when player moves X meters
[locationManager setDesiredAccuracy:kCLLocationAccuracyKilometer];//Nearest 1000 meters (.33 miles)
[locationManager startUpdatingLocation];

/*Map View*/
mapLoaded = false;
currentFilter = 0;
[_mapView setDelegate:self];
[_mapView setShowsUserLocation:YES];
[_mapView setMapType:MKMapTypeSatellite];

Location Manager Delegate:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    //Wait until User Location is initialized (NOT 0,0)
    NSLog(@"Latitude: %f, Longitude: %f",_mapView.userLocation.coordinate.latitude,_mapView.userLocation.coordinate.longitude);
    if(_mapView.userLocation.location.coordinate.latitude!=0.00 && _mapView.userLocation.location.coordinate.longitude!=0.00){

        //Update Player Object
        [player setLatitude:_mapView.userLocation.location.coordinate.latitude];
        [player setLongitude:_mapView.userLocation.location.coordinate.longitude];

        [player updatePlayerLocation];//Update location to network
        if(mapLoaded){//Map already loaded, call refresh
            [self refreshMap];
        }
        else{//First load of map
            [_mapView setCenterCoordinate:_mapView.userLocation.location.coordinate];
            [self populateMap];
            [self openVault];
            mapLoaded = true;//Disable map first load
            //timerMap = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(refreshMap) userInfo:nil repeats:YES];//Auto-Refresh of Map
        }
    }
}
Was it helpful?

Solution

You seem to be trying to get the location from the _mapView.userLocation CLLocation object.

You should rather use the last item in the locations array passed into your method.

see also documentation, guide with snippets

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top