2016-01-22 26 views
0

我想要使用Google Places API获取距离当前用户最近的位置并保存它,最终创建一个已保存位置的数组。由于谷歌文档在线不是很有帮助,我有一些麻烦,搞清楚如何自己做。我已经有一个CLLocationManager工作正常,但在同步所有内容时我感到茫然。最接近用户当前位置的抓取地点并将其保存到Google Places API

谢谢!

当用户按下按钮时,我希望它调用一个方法并保存最近的当前位置......这是迄今为止我所拥有的示例方法。

func saveInfoTestMethod() { 

    let placesClient = GMSPlacesClient?() 

    placesClient?.currentPlaceWithCallback({ (placeLikelihoods: GMSPlaceLikelihoodList?, error) -> Void in 
     if error != nil { 
      print("Current Place error: \(error!.localizedDescription)") 
      return 
     } 

     for likelihood in placeLikelihoods!.likelihoods { 
      if let likelihood = likelihood as? GMSPlaceLikelihood { 
       let place = likelihood.place 
       print("Current Place name \(place.name) at likelihood \(likelihood.likelihood)") 
       print("Current Place address \(place.formattedAddress)") 
       print("Current Place attributions \(place.attributions)") 
       print("Current PlaceID \(place.placeID)") 

       //Somehow work below code into here 

      } 
     } 
    }) 



    let userAddedPlace = GMSUserAddedPlace() 


    placesClient?.addPlace(userAddedPlace, callback: { (place, error) -> Void in 

     print(place!.name) 
     print(place!.formattedAddress) 
     print(place!.coordinate) 
     print(place!.types) 
     print(place!.phoneNumber) 

    }) 



} 
+0

你是什么意思“保存”一个地方?您是否试图保留用户所在地的日志? 'addPlace'不会对此有所帮助。 (这基本上是为Google地图添加地点,因此添加从Google Places API收到的地方不会起任何作用。) – spiv

+0

我的意思是从google API中检索地点并将其保存到数组中应用程序,分析或核心数据。这个文档在google上非常缺乏,所以我觉得有点失落。 – Echizzle

+0

我添加了Google Sign in框架/功能,是否可以使用此功能将用户的当前位置/附近地点的名称保存到当前的Google帐户?再次感谢。 – Echizzle

回答

0

我终于能想出解决办法,谷歌的文档不包括如何初始化GMSPlacesClient所以这就是被扔我了,下面的代码工作得很好。

func getInfoViaPlacesClient() { 

    let placesClient = GMSPlacesClient.sharedClient() 

    placesClient.currentPlaceWithCallback { (likelihoodlist, error) -> Void in 

     if error != nil { 
      print("Current Place error: \(error!.localizedDescription)") 
      return 
     } 

     for likelihood in likelihoodlist!.likelihoods { 

      let nearestPlace = likelihoodlist!.likelihoods.first 
      print(nearestPlace) 

      if let likelihood = likelihood as? GMSPlaceLikelihood { 
       let place = likelihood.place 
       print("Current Place name \(place.name) at likelihood \(likelihood.likelihood)") 
       print("Current Place address \(place.formattedAddress)") 
       print("Current Place attributions \(place.attributions)") 
       print("Current PlaceID \(place.placeID)") 
      } 
     } 
    } 

}