swift - 通过从 Parse 获取数据在 MKMapKit 上显示注释

标签 swift parse-platform mkmapview

所以我尝试使用用户当前位置创建注释。在将我的应用程序连接到 Parse 之前,我可以将信息保存到 NSUserDefaults 中,并且它工作得很好。现在我正在使用 Parse,我无法弄清楚如何在一个 View Controller 中调用在 Parse 中保存的坐标,以便我可以在不同的 map View Controller 中将坐标显示为注释。我当前使用的代码是

let userLoc = NSUserDefaults.StandardUserDefaults().dictionaryForKey("Location") as! [String : NSNumber]
Let userLat = userLoc["lat"]
Let userLng = userLoc["lng"]

var latitude:CLLocationDegrees = userLat as! CLLocationDegrees
var longitude:CLLocationDegrees = userLng as! CLLocationDegrees

那么我该如何修改这段代码才能显示我保存到 Parse 的坐标呢? 谢谢

最佳答案

我认为this answer就是您要找的

代码:

        //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()

关于swift - 通过从 Parse 获取数据在 MKMapKit 上显示注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31085968/

相关文章:

iOS 解析聊天应用程序 : Updating data from multiple pushes while in background/inactive.

ios - 在我的应用程序中向左/向右滑动会触发将数据保存到我的数据库的操作。然而,快速滑动会导致许多保存失败

ios - Parse.com - 保存新 PFObject 时出现错误 101

iphone - 如何将 MKMapView 用户位置蓝点更改为选择的图像?

swift - 缩放以适应用户位置和注释 map View 快速

ios - 如何更改 iOS 中内 TableView 单元格的单元格高度?

ios - UITextView 动态改变大小

swift - performSegue(withSender : sender:) does not work - no errors displayed

ios - Swift - 在 UIImageview 上设置可点击区域

ios - map 上的折线不可见