2015-10-21 50 views
0

我有一个地图和两个按钮。地图自动定位用户,如果他或她按下按钮1,它会保存当前位置以进行解析。这工作正常。但是,如果您在地图上的其他地方导航并放置一个别针,我希望在您点击按钮2时将该地点推送到解析,但它只是推动当前位置。丢弃引脚不推送位置数据使用Swift分析

我想我离我很近,我只是需要一点点帮助才能工作。

这里是我的代码

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

} 
+0

你可能只是包含处理你的代码问题,我对所做的一切有点困惑。 –

+0

这是处理地图的所有代码,我把所有其他的东西都拿出来了 – RubberDucky4444

+0

@IBAction func买家...是按钮#2,我不知道在那里放什么 – RubberDucky4444

回答

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时,该位置得到更新。

如果我明白了什么不对,请告诉我。

+0

谢谢,那个代码让人感觉很负荷! – RubberDucky4444

1

建立一个叫做lastPin或您设定为等于newPin一些财产。然后,当你打电话给买方功能时,基本上你在做另一个功能,除了使用myLocation以外,使用标记的位置。

+0

请问您能否详细说明最后一点部分。我试着做,但我得到错误 – RubberDucky4444

+0

我不知道足够的快速语法来为你写,但标记应该有一个位置或位置属性,有一个坐标,你可以从中获取经度和纬度。检查SDK的文档,你应该能够找到它。 –