2010-05-03 6 views
1

Existe-t-il un moyen de renvoyer l'emplacement des utilisateurs en tant que chaîne à partir d'un modèle?Retour d'un utilisateur en tant que chaîne iPhone

J'ai un modèle qui consiste à télécharger les mêmes données JSON à partir d'un service Web. Lors de l'envoi de ma demande, je dois ajouter? Lat = LAT_HERE & lng = LNG_HERE à la fin de la chaîne.

J'ai vu des tonnes d'exemples en utilisant la carte ou en mettant constamment à jour une étiquette. Mais je ne peux pas savoir comment retourner explicitement les valeurs lat et lng.

Im seulement 2 jours dans l'iPhone dev nous vous recommandons donc sur moi :)

Répondre

2

Vous devez tirer parti de Core Location, en particulier CLLocationManager. Apple ne fournit aucun guide de programmation CL, il suffit donc de regarder l'un des exemples comme LocateMe pour voir comment le faire.

1

Vous devez utiliser CLLocationManager comme ceci:

- (void)viewDidLoad 
    { 
     // this creates the CCLocationManager that will find your current location 
     CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease]; 
     locationManager.delegate = self; 
     locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; 
     [locationManager startUpdatingLocation]; 
    } 

    // this delegate is called when the app successfully finds your current location 
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
    { 
     // retrieve lat and lng in a string from newLocation.coordinate 
    NSString *lat = [NSString stringWithFormat:@"%d", newLocation.coordinate.latitude]; 
    NSString *lng = [NSString stringWithFormat:@"%d", newLocation.coordinate.longitude]; 
    } 

    // this delegate method is called if an error occurs in locating your current location 
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
    { 
    NSLog(@"locationManager:%@ didFailWithError:%@", manager, error); 
    } 
Questions connexes