Last active
October 28, 2020 08:07
-
-
Save imnaveensharma/fb41063c1f858da15199ee7545f51422 to your computer and use it in GitHub Desktop.
Implementation of Google's native GMSAutocompleteViewController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import GooglePlaces | |
//https://developers.google.com/places/ios-sdk/autocomplete | |
class GooglePlacePicker: NSObject { | |
private var getPlaceInfo:((_ place: String?, _ coordinate: CLLocationCoordinate2D?, _ isSuccess: Bool) -> Void)? | |
internal static let shared: GooglePlacePicker = { | |
GooglePlacePicker() | |
}() | |
func showAutocompleteViewControllerForPlaces(onViewController vc: UIViewController, completion: @escaping(_ place: String?, _ coordinate: CLLocationCoordinate2D?, _ success: Bool) -> Void) | |
{ | |
let autocompleteController = GMSAutocompleteViewController() | |
autocompleteController.delegate = self | |
if #available(iOS 13.0, *) { | |
if UIScreen.main.traitCollection.userInterfaceStyle == .dark { | |
autocompleteController.primaryTextColor = UIColor.white | |
autocompleteController.secondaryTextColor = UIColor.lightGray | |
autocompleteController.tableCellSeparatorColor = UIColor.lightGray | |
autocompleteController.tableCellBackgroundColor = UIColor.darkGray | |
} else { | |
autocompleteController.primaryTextColor = UIColor.black | |
autocompleteController.secondaryTextColor = UIColor.lightGray | |
autocompleteController.tableCellSeparatorColor = UIColor.lightGray | |
autocompleteController.tableCellBackgroundColor = UIColor.white | |
} | |
} | |
//--- Specify the place data types to return. ---// | |
let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) | UInt(GMSPlaceField.formattedAddress.rawValue) | UInt(GMSPlaceField.coordinate.rawValue) | | |
UInt(GMSPlaceField.placeID.rawValue))! | |
autocompleteController.placeFields = fields | |
//--- Specify a filter ---// | |
let filter = GMSAutocompleteFilter() | |
filter.type = .address | |
autocompleteController.autocompleteFilter = filter | |
self.getPlaceInfo = { place, coordinate, isSuccess in | |
completion(place, coordinate, isSuccess) | |
} | |
//--- Display the autocomplete view controller. ---// | |
vc.present(autocompleteController, animated: true, completion: nil) | |
} | |
} | |
// MARK: Extension :: GMSAutocompleteViewControllerDelegate | |
extension GooglePlacePicker: GMSAutocompleteViewControllerDelegate { | |
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) | |
{ | |
print("GooglePlacePicker :: Place Id :: \(place.placeID ?? "")") | |
print("GooglePlacePicker :: Place Name :: \(place.name ?? "")") | |
print("GooglePlacePicker :: Place Address :: \(place.formattedAddress ?? "")") | |
print("GooglePlacePicker :: Place Coordinate :: Latitude :: \(place.coordinate.latitude) And Longitude :: \(place.coordinate.longitude)") | |
guard let address = place.formattedAddress else { | |
self.getPlaceInfo?(nil, nil, false) | |
return | |
} | |
self.getPlaceInfo?(address, place.coordinate, true) | |
viewController.dismiss(animated: true, completion: nil) | |
} | |
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) | |
{ | |
print("GooglePlacePicker :: Error :: ", error.localizedDescription) | |
self.getPlaceInfo?(nil, nil, false) | |
} | |
func wasCancelled(_ viewController: GMSAutocompleteViewController) | |
{ | |
self.getPlaceInfo?(nil, nil, false) | |
viewController.dismiss(animated: true, completion: nil) | |
} | |
func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) { | |
//UIApplication.shared.isNetworkActivityIndicatorVisible = true | |
} | |
func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) { | |
//UIApplication.shared.isNetworkActivityIndicatorVisible = false | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func selectLocation() { | |
GooglePlacePicker.shared.showAutocompleteViewControllerForPlaces(onViewController: self) { (address, coordinate, isSuccess) in | |
//guard let strongSelf = self else { return } | |
if isSuccess && address != nil { | |
print("Selected Address :: \(address!)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment