3

我对 IOS 开发相当陌生,我几乎不知道这个错误意味着什么。当我尝试使用 Parse 获取距离用户 1.0 公里范围内的对象时,会出现错误。

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)
    }

但是,每当我使用 withinKilometers 约束时,下面的这个错误就会不断弹出,我不知道为什么会这样。任何帮助将不胜感激。

$nearSphere:仅适用于地理点字段(代码:102,版本:1.7.4)

4

2 回答 2

2

我的猜测是这是有效的,因为您的Name字段可能是字符串,而不是PFGeoPoint字段。如果您在 Parse 上查看数据浏览器,您将能够确定这一点。PFGeoPoint必须是数据浏览器中的纬度和经度才能使其正常工作。

于 2015-07-28T17:05:55.230 回答
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)

    }

}
于 2015-07-28T18:08:03.143 回答