2016-03-29 36 views
0

我有一个应用程序显示可以通过地图上的按钮添加一个tableview中的位置。我有麻烦保存这些解析(是的,我知道它正在退休,但我已经建立了一个新的服务器,工作完全相同,所以认为它是解析)还请注意我看过这个主题:https://stackoverflow.com/questions/30435362/how-can-i-send-coordinates-to-parse-and-save-it-as-parse-pfgeopoin吨,即使它看起来很简单,它仍然有我困惑所以这里是我的代码在我的tableview如何将位置(引脚)保存到解析服务器?

var places = [Dictionary<String,String>()] 

var activePlace = -1 

这里是代码的广告销(在地图视图控制器)

@IBAction func addSpot(sender: UIBarButtonItem) { 


     var newCoordinate2 = self.map.userLocation.location!.coordinate; 

     var location = CLLocation(latitude: newCoordinate2.latitude, longitude: newCoordinate2.longitude) 

     //title = "new address" 

     //try change order start 

     let annotation = MKPointAnnotation() 

     self.map.addAnnotation(annotation) 

     annotation.coordinate = newCoordinate2 

     annotation.title = title 


     annotation.coordinate = self.map.userLocation.location!.coordinate; 

     CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) -> Void in 


      var title = "" 

      if (error == nil) { 

       if let p = placemarks?[0] { 

        var subThouroughfare:String = "" 
        var thouroughfare:String = "" 

        if p.subThoroughfare != nil { 

         subThouroughfare = p.subThoroughfare! 

        } 

        if p.thoroughfare != nil { 

         thouroughfare = p.thoroughfare! 

        } 

        title = "\(subThouroughfare) \(thouroughfare)" 


       } 

      } 

      if title == "" { 

       title = "Added \(NSDate())" 

      } 

      places.append(["name":title,"lat":"\(newCoordinate2.latitude)","lon":"\(newCoordinate2.longitude)"]) 

      let annotation = MKPointAnnotation() 

      annotation.coordinate = newCoordinate2 

      annotation.title = title 

      self.map.addAnnotation(annotation) 

     } 

     self.map.addAnnotation(annotation); 





     func mapView(mapView: MKMapView!, 
        viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!{ 
      if(annotation is MKUserLocation){ 
       return nil; 
      } 

      // let pinView: Void = mapView.addAnnotation(annotation); 
      let pinAnnotationView = MKPinAnnotationView(annotation: annotation,reuseIdentifier:"MyIdentifier"); 
      return pinAnnotationView; 


     } 



    } 

谢谢您帮助的我是使用解析/外部服务器的新手

编辑:我曾尝试这样的:

PFObject["geoPoint"] = PFGeoPoint(location: location) 

    location[location] = activePlace 
    location.saveInBackgroundWithBlock { (succes, error) -> Void in 
     print("place has been saved") 
    } 

我上三行 1)实例成员“下标”不能在类型一起使用“PFObject” 2)输入“CLLocation”错误没有下标构件 3 )型CLLocation的值没有任何成员“saveInBackgroundWithBlock”

RE编辑:我曾尝试这样做,得到一个警告,说“返回后的代码不会被执行”也其上运行的应用程序,但不保存

let geoPoint = PFObject(className: "location") 

    geoPoint["location"] = activePlace 
    geoPoint.saveInBackgroundWithBlock { (succes, error) -> Void in 
     print("place has been saved") 
    } 
+0

你还没有解释你的代码在做什么错误 – Wain

+0

它没有做错任何事情,但它没有上传到解析。我只是不知道如何添加我必须解析的位置@Wain –

+0

您是否定义了一个PFObject来保存您的位置/每个位置? – Wain

回答

1

所以你需要一些东西来包含你的PFGeoPoint,那东西需要是PFObject。在解析后端,您可以为此对象创建表格,并使用对geoPoint的引用和其他任何您想要的内容进行设置。现在,您可以为每个新点使用表的类名实例化一个PFObject,设置对geoPoint的引用(使用pfObject["geoPoint"] = geoPoint),将其保存(使用pfObject.saveInBackgroundWithBlock...)并完成。

+0

我试过类似的东西(参见编辑的问题)它似乎没有工作,但我相信我没有做到你的意思 –

+0

'PFObject'实例和'PFGeoPoint'实例是不同的。 PFObject是PFGeoPoint存储的所有者和容器。你试图让它们变成同样的东西,但不会起作用 – Wain

+0

我再次编辑它的问题,这次我得到一个警告,说'返回后的代码永远不会被执行',并不保存,但我确定它更好? –

相关问题