简体   繁体   English

使用Parse nearGeoPoint检索用户位置

[英]Retrieving users location with Parse nearGeoPoint

I have an user table and every user has own location (PFGeoPoint object). 我有一个用户表,每个用户都有自己的位置(PFGeoPoint对象)。 Now what is I want to do is sorting and retrieving users from nearest to farthest location. 现在,我要做的是从最近到最远的位置对用户进行排序和检索。 The problem is some users doesnt have any location info(latitude, longitude, geopoint). 问题是某些用户没有任何位置信息(纬度,经度,地理位置)。 Their currentLocation column are empty. 它们的currentLocation列为空。 That's way query doesn't work as I expected. 这就是查询无法按我预期的方式工作的原因。

I put below code to save users location to parse 我在下面的代码中保存了用户要解析的位置

    [PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {

    NSLog(@"User is currently at %f, %f", geoPoint.latitude, geoPoint.longitude);
    self.userLocation = geoPoint;
    [[PFUser currentUser] setObject:geoPoint forKey:@"currentLocation"];
    [[PFUser currentUser] saveInBackground];

 }];

And put below code on my retrieve query method 并将以下代码放在我的检索查询方法中

PFGeoPoint *userGeoPoint = self.userLocation; //pass user location to PFGeoPoint

[query whereKey:@"currentLocation" nearGeoPoint:userGeoPoint]; //retrieve users from nearest to rarest 

I updated manually users geopoint information as latitude 0.0, longitude 0.0 if the user doesn't have geopoint info or doesn't let to app to use location. 我手动将用户的地理位置信息更新为纬度0.0,如果用户没有地理位置信息或不允许应用使用位置,则将经度设置为0.0。 Then I can retrieve users from nearest to farthest whether if they doesn't have geoopint information. 然后,我可以从最近到最远的用户检索是否没有地理信息。 And the code that I used for it in viewDidLoad is below. 我在viewDidLoad中使用的代码如下。

    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

if(status == kCLAuthorizationStatusAuthorized ||
   status == kCLAuthorizationStatusAuthorizedAlways ||
   status == kCLAuthorizationStatusAuthorizedWhenInUse) {

[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {

    NSLog(@"User is currently at %f, %f", geoPoint.latitude, geoPoint.longitude);
    self.userLocation = geoPoint;
    [[PFUser currentUser] setObject:geoPoint forKey:@"currentLocation"];
    [[PFUser currentUser] saveInBackground];

 }];
}

else if (status == kCLAuthorizationStatusNotDetermined ||
         status == kCLAuthorizationStatusRestricted ||
         status == kCLAuthorizationStatusDenied) {

    PFGeoPoint *geoPoint = [PFGeoPoint geoPointWithLatitude:0.0 longitude:0.0];

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

It sounds like you need to filter out users who don't have a geopoint. 听起来您需要过滤掉没有Geopoint的用户。

Try this: 尝试这个:

PFGeoPoint *userGeoPoint = self.userLocation; //pass user location to PFGeoPoint

[query whereKeyExists:@"currentLocation"]; //This is the key line you are missing
[query whereKey:@"currentLocation" nearGeoPoint:userGeoPoint];

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

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