Dropped Pin不使用Swift推送位置数据进行解析

时间:2015-10-21 00:32:26

标签: ios swift parse-platform

我有一张地图和两个按钮。地图自动定位用户,如果他或她按下按钮1,则保存当前位置以进行解析。这很好。但是,如果您在地图上的其他位置导航并放下一个引脚,我希望当您点击按钮2时该位置将被推送解析,但它只是推送当前位置。

我想我已经关闭了,我需要一些帮助才能让它发挥作用。

这是我的代码

    self.locationManager.delegate = self
    self.mapView.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    self.mapView.showsUserLocation = true
    let longPress = UILongPressGestureRecognizer(target: self, action: "mapLongPress:")
    longPress.minimumPressDuration = 1.5
    self.mapView.addGestureRecognizer(longPress)

var mapChangedFromUserInteraction = false

 func mapViewRegionDidChangeFromUserInteraction() -> Bool {
    let view = self.mapView.subviews[0]
    //  Look through gesture recognizers to determine whether this region change is from user interaction
    if let gestureRecognizers = view.gestureRecognizers {
        for recognizer in gestureRecognizers {
            if( recognizer.state == UIGestureRecognizerState.Began || recognizer.state == UIGestureRecognizerState.Ended ) {
                return true
            }
        }
    }
    return false
}


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    let location = locations.last

    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
    self.mapView.setRegion(region, animated: true)
    self.locationManager.stopUpdatingLocation()
}
func mapLongPress(recognizer: UIGestureRecognizer){
    print("its done")

    let touchedAt = recognizer.locationInView(self.mapView)
    let touchedAtCoordinate : CLLocationCoordinate2D = mapView.convertPoint(touchedAt, toCoordinateFromView: self.mapView)
    let newPin = MKPointAnnotation()
    newPin.coordinate = touchedAtCoordinate
    mapView.removeAnnotations(mapView.annotations)
    mapView.showsUserLocation = false
    mapView.addAnnotation(newPin)
}


@IBAction func photographer(sender: AnyObject, forEvent event: UIEvent) {
    let manager = CLLocationManager()
    let loc =  manager.location!.coordinate
    let actualLocation = PFGeoPoint(latitude:loc.latitude,longitude:loc.longitude)
    let object = PFObject(className:"User")
    object["Location"] = actualLocation
    object.saveInBackgroundWithBlock { (_success:Bool, _error:NSError?) -> Void in
        if _error == nil
        {
            // yay its saved
        }
    }
}

@IBAction func buyer(sender: AnyObject, forEvent event: UIEvent) {

}

2 个答案:

答案 0 :(得分:1)

创建一个名为lastPin的属性或您设置为等于newPin的属性。然后,当您调用买方函数时,基本上执行您在其他函数中执行的操作,除了使用myLocation而不是使用myLocation,请使用标记的位置。

答案 1 :(得分:1)

如果我理解你的问题,你可以这样做。

 //this would be an optional property that could have a value if there was a pin that was dropped
 var locationsOfPinDrop: PFGeoPoint?

func mapLongPress(recognizer: UIGestureRecognizer){

    let touchedAt = recognizer.locationInView(self.mapView)
    let touchedAtCoordinate : CLLocationCoordinate2D = mapView.convertPoint(touchedAt, toCoordinateFromView: self.mapView)
    let newPin = MKPointAnnotation()
    newPin.coordinate = touchedAtCoordinate
    mapView.removeAnnotations(mapView.annotations)
    mapView.showsUserLocation = false
    mapView.addAnnotation(newPin)

    //I added this line... It will store the last location that was pressed using the long press.
    self.locationOfPinDrop = PFGeoPoint(latitude:touchedAtCoordinate.latitude, touchedAtCoordinate:loc.longitude)
}

@IBAction func buyer(sender: AnyObject, forEvent event: UIEvent) {
     if let location = locationOfPinDrop {
          //there is a coordinate there, it is not nil.
            let user = PFObject(className:"User")
            user["Location"] = location
            user.saveInBackgroundWithBlock { (_success:Bool, _error:NSError?) -> Void in
                 if _error == nil {
                      //This resets the locationOfPinDrop to nil now that it is saved.
                      self.locationOfPinDrop = nil
                 }
            }
       }
}

我有点难以理解你的问题,但我相信我的答案可以解决它。我只创建了locatationOfPinDrop的存储属性,然后,当用户长按时,locationOfPinDrop设置为该坐标。现在,当按下按钮2 buyer时,此位置会更新。

如果我理解错了,请告诉我。