【问题标题】:how to retrive location from a PFGeopoint - (Parse.com and Swift) and show it on the map with Xcode 6.2如何从 PDF Geopoint 检索位置 -(Parse.com 和 Swift)并使用 Xcode 6.2 在地图上显示
【发布时间】:2015-06-14 11:32:05
【问题描述】:

对此有一些答案,但与不同的问题有关,并且都在objective-c中。 我将用户的位置保存在解析类“位置”中:

var locationManager = CLLocationManager()

var lat = locationManager.location.coordinate.latitude
var lon = locationManager.location.coordinate.longitude

let myGeoPoint = PFGeoPoint(latitude: lat, longitude:lon)
let myParseId = PFUser.currentUser().objectId //PFUser.currentUser().objectId

println("****** this is my geoPoint: \(myGeoPoint)")

func sendPosition(userOfPosition: User) {

let takePosition = PFObject(className: "Position")

 takePosition.setObject(myParseId, forKey: "who") //who
 takePosition.setObject(myGeoPoint, forKey: "where")
                                        takePosition.saveInBackgroundWithBlock(nil)

                                }


  sendPosition(currentUser()!)

所以这是我的结果:

然后我想在地图上显示它们,但是如何?不明白如何从“where”列中检索纬度和经度下面的代码不起作用:

   import UIKit
    import MapKit

    class MapViewController: UIViewController {


    @IBOutlet weak var mapView: MKMapView!

    var locationManager : CLLocationManager!


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.



        var locationManager = CLLocationManager()

        var lat = locationManager.location.coordinate.latitude
        var lon = locationManager.location.coordinate.longitude

        let location = CLLocationCoordinate2D(latitude: lat, longitude: lon)
        let span = MKCoordinateSpanMake(0.05, 0.05)
        let region = MKCoordinateRegionMake(location, span)
        mapView.setRegion(region, animated: true)

        let anotation = MKPointAnnotation()
        anotation.setCoordinate(location)
        anotation.title = "my title"
        anotation.subtitle = " my subtitle"

        mapView.addAnnotation(anotation)

        println("****** Welcome in MapViewController")



        //MARK: (471) Crossing Positions
        //*******************************************************

        let myGeoPoint = PFGeoPoint(latitude: lat, longitude:lon)
        let myParseId = PFUser.currentUser().objectId //PFUser.currentUser().objectId

        println("****** this is my geoPoint from map view controller: \(myGeoPoint)")


//        
//        var inspector = PFQuery(className:"GameScore")
//                inspector.saveInBackgroundWithBlock {
//                    (success: Bool, error: NSError?) -> Void in
//                    if (success) {
//                        // The object has been saved.
//                        var the = inspector.objectId
//                    } else {
//                        // There was a problem, check error.description
//                    }
//                }
//        
//        
//        


        func filterByProximity() {
            PFQuery(className: "Position")
                .whereKey("where", nearGeoPoint: myGeoPoint, withinKilometers: 500.0)     //(474)
            .findObjectsInBackgroundWithBlock ({
                objects, error in
                if let proximityArray = objects as? [PFObject] {
                    println("****** here the proximity matches: \(proximityArray)")
                    for near in proximityArray {
                        println("here they are \(near)")
                        if let position = near["where"] as! PFGeoPoint {
                        let theirLat = position.latituide
                        let theirLon = position.longitude
                    }

                        let theirLat = near["where"].latitude as Double
                        let theirlong = near["where"].longitude as Double
                        let location = CLLocationCoordinate2DMake(theirLat, theirlong)
                        let span = MKCoordinateSpanMake(0.05, 0.05)
                        let region = MKCoordinateRegionMake(location, span)
                        self.mapView.setRegion(region, animated: true)
                        let theirAnotation = MKPointAnnotation()
                        theirAnotation.setCoordinate(location)
                        self.mapView.addAnnotation(anotation)

                    }
                }
            })
        }

        filterByProximity()


//        //update my position
//        
//        func exists() {
//        PFQuery(className:"Position")
//            .whereKey("who", containsString: myParseId)
//            .findObjectsInBackgroundWithBlock({
//            thisObject, error in
//                if let result = thisObject as? [PFObject] {
//                    println("here the result: \(result)")
//                    
//                    let gotTheId = result[0].objectId
//                    println("ecco l'id singolo \(gotTheId)")
//
//                            //******** update function ********
//                            var query = PFQuery(className:"Position")
//                            query.getObjectInBackgroundWithId(gotTheId) {
//                                (usingObject: PFObject?, error: NSError?) -> Void in
//                                if error != nil {
//                                    println(error)
//                                } else if let objectToupdate = usingObject {
//                                    println("else occurred")
//                                    objectToupdate["where"] = myGeoPoint
//                                    println("position should be updated")
//                                    objectToupdate.saveInBackgroundWithBlock(nil)
//                                    println("position should be saved")
//
//                                }
//                            }
//                            //******** end update function ********
//                }
//            })
//        }
//        
//        exists()

        //*******************************************************




    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }




}

第一次回答后更新

尝试检查选项,但收到以下消息: 在没有向下铸造双重之后:

【问题讨论】:

  • near["where"] 是一个 PFGeoPoint 对象。该对象具有纬度和经度属性
  • 如果使用 let theirLat = near["where"].latitude as Double let theirlong = near["where"].longitude as Double 我遇到线程错误,你能发布代码吗?
  • near["where"] 将返回一个可选值。你需要打开它并检查可能的 nil
  • 试过这个,不起作用,但我是 Swift 的新手,并且选项现在是一个问题:让 theirLat = near["where"].latitude as? Double let theirlong = near["where"].longitude as?如果他们的Lat == nil,则加倍 || theirlong == nil { println("error") } else {
  • 你应该使用if let where=near["where"] as! PFGeopoint { let theirLat=where.latituide let theirLon=where.longitude }

标签: ios swift parse-platform


【解决方案1】:

'where' 是一个 PFGeoPoint,你可以在它们上调用纬度和经度

试试这样的方法 - 我还不能 100% 确定 Swift 语法

if(geoPoint)
{
   if(geoPoint!.latitude)
   {
       let latitude = geoPoint!.latitude as double;
   }

}

【讨论】:

【解决方案2】:

这行得通。既是可选问题,也是变量问题,我使用了错误的:

        //MARK: (471) Crossing Positions
        //*******************************************************

        let myGeoPoint = PFGeoPoint(latitude: lat, longitude:lon)
        let myParseId = PFUser.currentUser().objectId //PFUser.currentUser().objectId
        var radius = 100.0

        println("****** this is my geoPoint from map view controller: \(myGeoPoint)")


        //MARK: *** let's look for other users ***

        var nearArray : [CLLocationCoordinate2D] = []

        func filterByProximity() {
            PFQuery(className: "Position")
                .whereKey("where", nearGeoPoint: myGeoPoint, withinKilometers: radius)     //(474)
                .findObjectsInBackgroundWithBlock ({
                    objects, error in
                    if let proximityArray = objects as? [PFObject] {
//                        println("****** here the proximity matches: \(proximityArray)")
                        for near in proximityArray {
//                            println("here they are \(near)")

                            let position = near["where"] as? PFGeoPoint

                            var theirLat = position?.latitude       //this is an optional
                            var theirLong = position?.longitude     //this is an optional
                            var theirLocation = CLLocationCoordinate2D(latitude: theirLat!, longitude: theirLong!)

                            nearArray.append(theirLocation)

                            if nearArray.isEmpty {
                                println("*** ERROR! anyone close by ***")
                            } else
                            {
                                for person in nearArray {

                                    let span = MKCoordinateSpanMake(2.50, 2.50)
                                    let region = MKCoordinateRegionMake(theirLocation, span)
                                    self.mapView.setRegion(region, animated: true)

                                    let theirAnotation = MKPointAnnotation()
                                    theirAnotation.setCoordinate(theirLocation)
                                    theirAnotation.title = near["who"] as String

                                    self.mapView.addAnnotation(theirAnotation)
                                }

                            }



                        }
                        println("****** in a radius of \(radius) there are \(nearArray.count) bikers ******")

                    }
                })
        }

        filterByProximity()

【讨论】:

    猜你喜欢
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    相关资源
    最近更新 更多