Trovare posizione dell'utente prima mappa carichi

voti
1

Sto cercando di trovare la posizione dell'utente tramite:

CLLocationCoordinate2D _location;
CLLocation *userLoc = nil;
if(appDelegate.clLocationManager.locationServicesEnabled){
    userLoc = mapView.userLocation.location;
    _location = userLoc.coordinate;
}

Il problema è che non ho ancora inizializzato la mappa in modo quanto sopra non funziona. C'è un modo per ottenere la posizione dell'utente senza utilizzare MapKit? O devo essere utilizzando una tecnica diversa?

È pubblicato 12/03/2010 alle 21:34
fonte dall'utente
In altre lingue...                            


2 risposte

voti
1

Per ottenere la posizione dell'utente è necessario in realtà "start" il location manager prima di provare a leggere le informazioni sulla posizione. Suggerisco che si guarda LocateMe campione da Apple.

LocateMe codice di esempio

In poche parole, le informazioni sulla posizione ottenendo dell'utente può essere effettuata in due fasi:

- (void)viewDidLoad {
    [[self locationManager] startUpdatingLocation];
}

- (CLLocationManager *)locationManager { 

    if (locationManager != nil) {       
        return locationManager; 
    }

    NSLog(@"%s, Creating new Location Manager instance.", __FUNCTION__);
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = DISTANCE_FILTER_VALUE;
    locationManager.delegate = self; 

    return locationManager; 
}

... e poi,

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    NSLog(@"%s, location manager returned a new location.", __FUNCTION__);

    // Check if location is valid (or good enough for us), 
    // and then process the location
    if ([self locationIsValid:newLocation]) {
            [self processLocation:newLocation];

            // Very important! When your done with it, stop the location manager
            [[self locationManager] sopUpdatingLocation];

            // (Maybe even) clear it
            locationManager.delegate = nil;
            [locationManager release];
            locationManager = nil;
    }
}

Spero che questo ti aiuti!

Risposto il 19/04/2010 a 11:46
fonte dall'utente

voti
0

Ho quasi cancellato questa domanda, ma vedo che qualcuno ha dato un voto alto. Questa sembra essere la risposta:

CLLocationCoordinate2D _location;
CLLocation *userLoc = nil;
if(appDelegate.clLocationManager.locationServicesEnabled){
    userLoc = appDelegate.clLocationManager.location;
    _location = userLoc.coordinate;
}

AppDelegate è solo un riferimento al delegato applicazione, dove vive la mia location manager esempio.

Risposto il 12/03/2010 a 21:41
fonte dall'utente

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more