3

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)

rici
  • 234,347
  • 28
  • 237
  • 341
bantukatanaya
  • 93
  • 1
  • 10

2 Answers2

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.

pbush25
  • 5,228
  • 2
  • 26
  • 35
1

I would not use the withinKilometers. Instead I would use the withinMiles method. It is a lot easier for using geopoints from Parse in Swift. If you still want to use Kilometers just convert Kilometers to Miles. In this case instead of:

withinKilometers: 1.0

use:

withinMiles: 0.62

This code below should work:

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

If the code above does not work:

try this out, this is the coding that I used to display points on a map that is a set couple miles away from the users current location. But you will have to set up some things in the info.plist file to get the users current location. Here is the example code that you can try out.

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)

    }

}
Stefan DeClerck
  • 1,162
  • 2
  • 12
  • 22