简体   繁体   English

通过解析获取用户位置

[英]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. 当我给数据库提供一个预设的经度并且可以长时间使用时,我的数据库可以正确更新,但是当我使用解析代码来获取用户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. 这是因为我做错了吗,还是因为在计算机上使用iPhone模拟器时无法正常工作。 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 您也可以看看iOS 地理位置指南

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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