2015-10-21 52 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的文檔,你應該能夠找到它。 –