简体   繁体   中英

Getting the Users location with parse

I am trying to make my code that on app loading it updates my parse database with the users current time and location. Here is my current code:


- (void)viewDidLoad {
    [super viewDidLoad];
   PFObject *testObject = [PFObject objectWithClassName:@"TestObject"];
   testObject[@"foo"] = @"bar";
//    PFGeoPoint *point = [PFGeoPoint geoPointWithLatitude:40.0 longitude:-30.0];
//    testObject[@"location"] = point;
    [PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
        if (!error) {
            testObject[@"location"] = geoPoint;
        }
    }];

    [testObject saveInBackground];

My database updates correctly when I give it a preset lat and long to use but it doesnt work when I use parses code to fetch the users geoPoint. Is this because I am doing this wrong or is it because it will not work when im using the iPhone simulator on my computer. Thanks.

Example code is given in the parse blog :

[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
    if (!error) {
        NSLog(@"User is currently at %f, %f", geoPoint.latitude, geoPoint.longitude);

        [[PFUser currentUser] setObject:geoPoint forKey:@"currentLocation"];
        [[PFUser currentUser] saveInBackground];
    }
}];

As per my opinion. You have to save the location from the completion block. you have done it from the outside of the block.

Also you can take a look of iOS Guide for Geo Location

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM