无法使用Swift和Parse db更新GMaps上的标记

时间:2016-05-25 06:05:02

标签: ios swift google-maps parse-platform gmsmapview

我编写了一个功能,可以从Parse DB中获取数据并在Google Maps上显示标记。在我的 ViewDidLoad 功能中,我有 NSTimer ,它每秒都会调用该函数。我遇到的问题是 SpotNames SpotGeopoints 数组在每次函数调用后都会不断追加并增长。此外,如果 ParkingSpotIsEmpty 值为false,我无法在地图上实时删除标记。

有任何建议如何提高效率吗?

func displayParkingSpots() {

        let query:PFQuery = PFQuery(className: "parkingLocations")
        // query.whereKey("ParkingSpotIsEmpty", equalTo: true)
        query.findObjectsInBackgroundWithBlock { (objects:[PFObject]?, error: NSError?) -> Void in
            if !(error != nil) { for object in objects! {

                if (object["ParkingSpotIsEmpty"] as! Int == 1) {
                    self.SpotNames.append(object["SpotNames"] as! String)
                    self.SpotGeoPoints.append(object["longitude"] as! PFGeoPoint)
                    self.SpotLocationLatitudes.append(self.SpotGeoPoints.last?.latitude as CLLocationDegrees!)
                    self.SpotLocationLongitudes.append(self.SpotGeoPoints.last?.longitude as CLLocationDegrees!)
                    print(self.SpotNames)
                    let parkingSpotsPosition = CLLocationCoordinate2D(latitude: self.SpotLocationLatitudes.last!, longitude: self.SpotLocationLongitudes.last!)
                    let marker = GMSMarker(position: parkingSpotsPosition)
                    marker.title = self.SpotNames.last
                    //marker.icon = GMSMarker.markerImageWithColor(UIColor.greenColor())
                    marker.icon = UIImage(named: "parking-location")
                    marker.tracksInfoWindowChanges = true
                    marker.map = self.gMapsView
                }

                }
            }
        }

    }

override func viewDidLoad() {
        super.viewDidLoad()

        displayParkingSpots()
        gameTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(displayParkingSpots), userInfo: nil, repeats: true)

        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()

        gMapsView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
    }

0 个答案:

没有答案