简体   繁体   中英

How to get GMSPlace for a CLLocationCoordinate2D?

I have a location coordinate in the form of a CLLocationCoordinate2D . How do I get its equivalent GMSPlace object using the Google Maps SDK?

This seems like it should be a very simple task, but I couldn't find anything in Google's documentation or on Stack Overflow.

I am working on a similar issue, and I haven't found the exact solution but these alternatives may work depending on your situation. If you are okay with having a GMSAddress instead of a GMSPlace , you may use a GMSGeocoder with a call to reverseGeocodeCoordinate as seen in option two below.

Two options if you're trying to get the user's current location:

  1. Use Google Maps current location to get a GMSPlace. This is pretty simple and solves your problem if you are okay with only resorting to actual places. The problem with this is that I couldn't figure out how to get all addresses (as opposed to businesses). You can see the documentation here .

    In viewDidLoad:

     let placesClient = GMSPlacesClient() 

    When you want to get the current place:

     placesClient?.currentPlaceWithCallback({ (placeLikelihoods, error) -> Void in if error != nil { // Handle error in some way. } if let placeLikelihood = placeLikelihoods?.likelihoods.first { let place = placeLikelihood.place // Do what you want with the returned GMSPlace. } }) 
  2. Use OneShotLocationManager to get the CLLocationCoordinate2D and turn it into a GMSAddress. You will have to replace the _didComplete function with the code below to return a GMSAddress instead of a CLLocationCoordinate2D.

     private func _didComplete(location: CLLocation?, error: NSError?) { locationManager?.stopUpdatingLocation() if let location = location { GMSGeocoder().reverseGeocodeCoordinate(location.coordinate, completionHandler: { [unowned self] (response, error) -> Void in if error != nil || response == nil || response!.firstResult() == nil { self.didComplete?(location: nil, error: NSError(domain: self.classForCoder.description(), code: LocationManagerErrors.InvalidLocation.rawValue, userInfo: nil)) } else { self.didComplete?(location: response!.firstResult(), error: error) } }) } else { self.didComplete?(location: nil, error: error) } locationManager?.delegate = nil locationManager = nil } 

Someone posted on here a convenient wrapper to extract fields from the GMSAddressComponents that you may find useful when dealing with this API. This makes it easy because then when you want to access the city all you have to do is place.addressComponents?.city as an example.

extension CollectionType where Generator.Element == GMSAddressComponent {
  var streetAddress: String? {
      return "\(valueForKey("street_number")) \(valueForKey(kGMSPlaceTypeRoute))"
  }

  var city: String? {
      return valueForKey(kGMSPlaceTypeLocality)
  }

  var state: String? {
      return valueForKey(kGMSPlaceTypeAdministrativeAreaLevel1)
  }

  var zipCode: String? {
      return valueForKey(kGMSPlaceTypePostalCode)
  }

  var country: String? {
      return valueForKey(kGMSPlaceTypeCountry)
  }

  func valueForKey(key: String) -> String? {
      return filter { $0.type == key }.first?.name
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM