Getting objects based on distance filter in Parse+Swift

时间:2015-07-28 16:31:46

标签: swift parse-platform geopoints

I'm fairly new to IOS development and I have little to no clue what this error means. The error shows up when I try to use Parse to get an object within a 1.0 km distance from the user.

PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint: PFGeoPoint?, error: NSError?) -> Void in

    var query = PFQuery(className: "VenueData")
    query.whereKey(key: "Name", nearGeoPoint: geoPoint, withinKilometers: 1.0)

    var object = query.findObjects()
    println(object)
    }

However, whenever i use the withinKilometers constraint this error below keeps popping up and I do not know why that happens. Any help would be greatly appreciated.

$nearSphere: only works on geopoints fields (Code: 102, Version: 1.7.4)

2 个答案:

答案 0 :(得分:2)

My guess is this is working because your Name field is likely a string, not a PFGeoPoint field. If you look in your data browser on Parse you'll be able to determine this. PFGeoPoint has to be a latititude and a longitude in the data browser in order for this to work.

答案 1 :(得分:1)

我不会使用withinKilometers。相反,我会使用withinMiles方法。 使用Swift中的Parse的地理输出要容易得多。 如果您仍想使用公里,只需将公里数转换为英里数。在这种情况下,而不是:

withinKilometers: 1.0

使用:

withinMiles: 0.62

以下代码应该有效:

query.whereKey("Name", nearGeoPoint: geoPoint, withinMiles: 0.62)

如果上面的代码不起作用:

试试这个,这是我用来在地图上显示距离用户当前位置几英里的点的编码。但是您必须在info.plist文件中设置一些内容以获取用户当前位置。以下是您可以尝试的示例代码。

import UIKit
import MapKit
import CoreLocation
import Parse
import ParseUI
import Bolts

var currentLoc: PFGeoPoint = PFGeoPoint()

class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!
    var query: PFQuery = PFQuery()
    var manager:CLLocationManager!

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)

        query = PFQuery(className: "ClassName")
        query.whereKey("dateOfClass", greaterThanOrEqualTo: NSDate())
        query.whereKey("classes", nearGeoPoint: currentLoc, withinMiles: 400)

        query.findObjectsInBackgroundWithBlock {
            (posts, error) -> Void in
            if error == nil {

                let myPosts = posts as! [PFObject]

                for post in myPosts {

                    var subtitleString: String = String()

                    if let dateObject = post["dateOfClass"] as? NSDate {

                        var dateFormatter = NSDateFormatter()
                        dateFormatter.dateFormat = "MM/dd/yyyy HH:mm a"
                        var dateNSDate: String = dateFormatter.stringFromDate(dateObject)
                        subtitleString = dateNSDate

                    }

                    let point = post["classes"] as! PFGeoPoint

                    var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: point.latitude, longitude: point.longitude)
                    var workoutClassName: String = post.objectForKey("workoutClassName") as! String
                    var workoutClassInstructor: String = post.objectForKey("instructorName") as! String
                    var objectsID: String = post.objectId! as String
                    var annotation: MapPin = MapPin(coordinate: coordinate, title: "\(workoutClassName), \(workoutClassInstructor)", subtitle: "\(subtitleString)", annotationID: "\(objectsID)")
                    self.mapView.addAnnotation(annotation)

                }
            } else {
                // Log details of the failure
                println("Error: \(error)")
            }
        }

        println(currentLoc)

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        mapView.showsUserLocation = true

        var latitude: CLLocationDegrees = currentLoc.latitude

        var longitude: CLLocationDegrees = currentLoc.longitude

        var latDelta:CLLocationDegrees = 0.7

        var lonDelta:CLLocationDegrees = 0.7

        var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

        var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

        var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

        mapView.setRegion(region, animated: false)

    }

}