2014-09-19 54 views
0

我在我的数据云解析中添加了一个“位置”类型的GeoPoint。 我用这个方法来获取用户当前位置:`地理定位解析

- (IBAction)insertCurrentLocation:(id)sender { 
// If it's not possible to get a location, then return. 
CLLocation *location = self.locationManager.location; 
if (!location) { 
    return; 
} 

// Configure the new event with information from the location. 
CLLocationCoordinate2D coordinate = [location coordinate]; 
PFGeoPoint *geoPoint = [PFGeoPoint geoPointWithLatitude:coordinate.latitude longitude:coordinate.longitude]; 
PFObject *object = [PFObject objectWithClassName:@"Recipe"]; 
[object setObject:geoPoint forKey:@"Location"]; 

[object saveEventually:^(BOOL succeeded, NSError *error) { 
    if (succeeded) { 
     // Reload the PFQueryTableViewController 
     [self loadObjects]; 
    } 
}]; 

}

我有这样的方法来获得(在我的数据云柱位置)餐厅地点的位置:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object { 
// A date formatter for the creation date. 
static NSDateFormatter *dateFormatter = nil; 
if (dateFormatter == nil) { 
    dateFormatter = [[NSDateFormatter alloc] init]; 
    dateFormatter.timeStyle = NSDateFormatterMediumStyle; 
    dateFormatter.dateStyle = NSDateFormatterMediumStyle; 
} 

static NSNumberFormatter *numberFormatter = nil; 
if (numberFormatter == nil) { 
    numberFormatter = [[NSNumberFormatter alloc] init]; 
    numberFormatter.numberStyle = NSNumberFormatterDecimalStyle; 
    numberFormatter.maximumFractionDigits = 3; 
} 

PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"LocationCell"]; 

// Configure the cell 
PFGeoPoint *geoPoint = object[@"Location"]; 

cell.textLabel.text = [dateFormatter stringFromDate:object.updatedAt]; 

NSString *string = [NSString stringWithFormat:@"%@, %@", 
        [numberFormatter stringFromNumber:[NSNumber numberWithDouble:geoPoint.latitude]], 
        [numberFormatter stringFromNumber:[NSNumber numberWithDouble:geoPoint.longitude]]]; 

cell.detailTextLabel.text = string; 

return cell; 

}

现在我想知道如何计算距离进入位置的用户和餐馆的用户。

我有这样的方法,但我不知道在哪里可以插入此方法

//Get the user's current location 
PFGeoPoint.geoPointForCurrentLocationInBackground { 
(point:PFGeoPoint!, error:NSError!) -> Void in 
if error == nil { 

//点包含用户的当前点

//Get a max of 100 of the restaurants that are within 5km, 
    //ordered from nearest to furthest 
    var query = PFQuery(className: "restaurants") 
    query.limit = 100 
    query.whereKey("location", nearGeoPoint: point, withinKilometers: 5.0) 
    query.findObjectsInBackgroundWithBlock{ 
     (objects: [AnyObject]!, error: NSError!) -> Void in 
     if (error == nil) { 
      //objects contains the restaurants 
     } 
    } 
} 

}

它是好方法?在哪里插入她?请帮我 谢谢

+0

这是我给你的完全相同的答案[here](http://stackoverflow.com/questions/25923761/locations-around-the-users-with-parse/25924345#25924345)。你可以把它放在viewDidLoad中,但是一旦它执行,你就必须对它做些什么。 – Dehli 2014-09-19 18:01:56

+0

Plz这个函数不能在ViewDidLoad中帮助我plz – 2014-09-21 12:45:38

+0

我有这个错误“属性”geoPointForCurrentLocationBackground“找不到对象的类型”PFGeoPoint“ – 2014-09-21 12:47:42

回答

0

我给你的功能here可以去viewDidLoad:。您找不到该物业的原因是您必须为Info.plist添加一个密钥,以便您的手机知道它需要了解您的位置。

在您的Info.plist中,添加一行以下值NSLocationWhenInUseUsageDescription。然后在右侧给它一个说明,说:“为了使应用程序工作,我们需要知道你的位置”。现在你可以使用geoPointForCurrentLocationInBackground

+0

如果这有效,请务必检查我的答案是否正确。 – Dehli 2014-09-21 15:17:59