Reputation: 1077
UPDATE - Found the Solution and Here is the Sample Project if anyone looking for the same solution. This will get City, Country, Postal code and LocalAdress(subLocality) from the coordinates and nearby locations. https://github.com/SwiftGuides/Google_Place_Picker
I have implemented google place picker API in my project so that I can move picker and get nearby locations and also the location of the exact street where my picker is located.
I want the address of place picker custom location like (unnamed road, Panchkula, India) instead of (37°19'1464"N 122°01'74.724"W).
In android google place picker API when we Click "select this address" for custom location, It shows a popup and sets the location in string format (unnamed road, Panchkula, India) instead of (37°19'1464"N 122°01'74.724"W)
I want something like that
Here is an image of the current status
Please help !!
import UIKit
import GooglePlaces
import GooglePlacePicker
import GoogleMaps
class ViewController: UIViewController,GMSPlacePickerViewControllerDelegate {
@IBOutlet weak var addressLabel: UILabel!
@IBOutlet weak var placeNameLabel: UILabel!
@IBOutlet weak var coordinatesLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
// To receive the results from the place picker 'self' will need to conform to
// GMSPlacePickerViewControllerDelegate and implement this code.
func placePicker(_ viewController: GMSPlacePickerViewController, didPick place: GMSPlace) {
// Dismiss the place picker, as it cannot dismiss itself.
viewController.dismiss(animated: true, completion: nil)
print("Place name \(place.name)")
print("PostalCode\(place.addressComponents)")
print("Place address \(place.formattedAddress)")
print("Place attributions \(place.attributions)")
placeNameLabel.text = place.formattedAddress
//Get address Seperated like city,country,postalcode
let arrays : Array = place.addressComponents!
for i in 0..<arrays.count {
let dics : GMSAddressComponent = arrays[i]
let str : String = dics.type
if (str == "country") {
print("Country: \(dics.name)")
}
else if (str == "administrative_area_level_1") {
print("State: \(dics.name)")
}
else if (str == "administrative_area_level_2") {
print("City: \(dics.name)")
}
else if (str == "postal_code"){
print("PostalCode:\(dics.name)")
addressLabel.text = dics.name // this is only to get postalcode of the selected nearby location
}
}
}
func placePickerDidCancel(_ viewController: GMSPlacePickerViewController) {
// Dismiss the place picker, as it cannot dismiss itself.
viewController.dismiss(animated: true, completion: nil)
print("No place selected")
}
@IBAction func pickPlaceButton(_ sender: Any) {
let config = GMSPlacePickerConfig(viewport: nil)
let placePicker = GMSPlacePickerViewController(config: config)
//This delegate has to be called here to proper working of cancle and selected location feature , it is not mentioned in the official documentation
placePicker.delegate = self
present(placePicker, animated: true, completion: nil)
}
}
Upvotes: 0
Views: 1970
Reputation: 141
Just get the location from the marker and call the below function which will return a return Address string
func getPlaceAddressFrom(location: CLLocationCoordinate2D, completion: @escaping (_ address: String) -> Void) {
let geocoder = GMSGeocoder()
geocoder.reverseGeocodeCoordinate(location) { response, error in
if error != nil {
print("reverse geodcode fail: \(error!.localizedDescription)")
} else {
guard let places = response?.results(),
let place = places.first,
let lines = place.lines else {
completion("")
return
}
completion(lines.joined(separator: ","))
}
}
}
Upvotes: 1