Het vinden van locatie van de gebruiker voor kaartweergaven

stemmen
1

Ik ben op zoek naar de locatie van de gebruiker met behulp van vinden:

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

Het probleem is dat ik nog niet de kaart geïnitialiseerd zodat de bovenstaande niet werkt. Is er een manier om de locatie van de gebruiker te krijgen zonder gebruik te maken mapkit? Of moet ik met behulp van een andere techniek?

De vraag is gesteld op 12/03/2010 om 21:34
bron van user
In andere talen...                            


2 antwoorden

stemmen
1

Om de locatie van de gebruiker te komen moet je eigenlijk de locatie manager "start" voordat je probeert om de locatie informatie te lezen. Ik stel voor dat je kijkt naar LocateMe monster van Apple.

LocateMe Sample Code

In een notendop, kan locatie-informatie krijgen van de gebruiker worden gedaan in twee stappen:

- (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; 
}

... en dan,

- (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;
    }
}

Ik hoop dat dit helpt!

antwoordde op 19/04/2010 om 11:46
bron van user

stemmen
0

Ik bijna verwijderde deze vraag maar zie iemand gaf het een up vote. Dit lijkt het antwoord:

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

appDelegate is slechts een verwijzing naar de toepassing afgevaardigde, waar mijn locatiemanager instantie leeft.

antwoordde op 12/03/2010 om 21:41
bron van user

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