【问题标题】:Grabbing place nearest to user's current location and saving it to Google Places API抓取离用户当前位置最近的地点并将其保存到 Google Places API
【发布时间】:2016-01-22 00:14:58
【问题描述】:

我想使用 Google Places API 获取离当前用户最近的位置并保存它,最终创建一组保存的位置。由于在线谷歌文档不是很有帮助,我在自己弄清楚如何做这件事上遇到了一些麻烦。我已经有一个 CLLocationManager 工作正常,但是在同步所有内容时我不知所措。

谢谢!

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

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)

    })



}

【问题讨论】:

  • “保存”一个地方是什么意思?您是否尝试记录用户去过的地方? addPlace 不会对此有所帮助。 (基本上,它是用于向 Google 地图添加地点,因此添加从 Google Places API 收到的地点不会有任何用处。)
  • 我的意思是从 google API 中检索一个位置并将它们保存到应用程序内的一个数组中,无论是 Parse 还是 Core Data。谷歌的文档非常缺乏,所以我觉得有点失落。
  • 我添加了 Google 登录框架/功能,是否可以使用此功能将用户的当前位置/附近地点的名称保存到当前的 google 帐户?再次感谢。

标签: ios swift2 google-places-api


【解决方案1】:

我终于能够弄清楚这一点,谷歌文档不包括如何初始化 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)")
            }
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    相关资源
    最近更新 更多