How to limit Google Places Autocomplete to not include Address? I currently have my setup
class FilterVC: UIViewController, GMSAutocompleteViewControllerDelegate {
// MARK: SEARCH BAR
#IBAction func searchBarAction(_ sender: Any) {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
placeAutocomplete(resultsViewController: autocompleteController)
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().tintColor = UIColor.hiGreyishBrownTwo
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.hiGreyishBrownTwo]
present(autocompleteController, animated: true, completion: nil)
}
func placeAutocomplete(resultsViewController: GMSAutocompleteViewController) {
var placeClient = GMSPlacesClient()
let filter = GMSAutocompleteFilter()
filter.type = .city
filter.country = "USA"
resultsViewController.autocompleteFilter = filter
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
DataService.instance.place = place
FillAddress(place: place)
fillAddressForm()
print(DataService.instance._address_line1)
print(DataService.instance._city)
print(DataService.instance._postalCode)
print(DataService.instance._state)
print(DataService.instance._country)
DataService.instance.addressLabel = place.formattedAddress
dismiss(animated: true, completion: nil)
}
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
print("ERROR \(error) Autocomplete")
}
I tried using a GMSAutocompleteFilter to limit my results to only include state city, zip, country. I don't want to display address in the autocomplete controller. When I do this, it only displays Country and city, I can't enter zip code. How would I make that available? I'm not quite sure what I'm missing or what the next step to take is. Any suggestions would be much appreciated.
I was going to take this off since I found a solution, but just in case some one runs into a similar issue, a very easy fix is to set filter type to region.
func placeAutocomplete(resultsViewController: GMSAutocompleteViewController) {
var placeClient = GMSPlacesClient()
let filter = GMSAutocompleteFilter()
filter.country = "USA"
filter.type = .region
resultsViewController.autocompleteFilter = filter
This will limit the search to not include address. When trying .city, that eliminated to much. Well hope this helps someone in the future.
Related
hello everyone I install the alamofire and google places pod to autocomplete the places search
but it gives me the error:
The “Swift Language Version” (SWIFT_VERSION) build setting must be set to a
supported value for targets that use Swift. This setting can be set in the
build settings editor.
my code:
class SetLocationViewController: UIViewController {
private var placesClient = GMSPlacesClient()
#IBOutlet weak var setLocationTf: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
placesClient = GMSPlacesClient.shared()
}
#IBAction func onClickTf(_ sender: Any) {
setLocationTf.resignFirstResponder()
let acController = GMSAutocompleteViewController()
acController.delegate = self
let filter = GMSAutocompleteFilter()
filter.type = .establishment
filter.countries = ["BR"]
acController.autocompleteFilter = filter
let field: GMSPlaceField = [.name, .placeID]
acController.placeFields = field
present(acController, animated: true, completion: nil)
}
}
extension SetLocationViewController: GMSAutocompleteViewControllerDelegate {
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
if let name = place.name {
setLocationTf.text = name
}
dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
print("Error: ", error.localizedDescription)
}
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
dismiss(animated: true, completion: nil)
}
}
The error I got:
I am trying to do Google Autocomplete using Google Places in Swift 3.0. But I need to search depending upon my current location. Example, If I am in Kolkata, India and I type search keyword "Ko" it will show the results of Kolkata first .
Can anyone help me.
Here is my code.I import GooglePlaces in my class
#IBAction func txtFieldLocationDidStartEditing(_ sender: Any) {
self.placeAutocomplete()
}
func placeAutocomplete() {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
present(autocompleteController, animated: true, completion: nil)
}
// MARK: - autoComplete Delegates
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(place.name)")
dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}
// User canceled the operation.
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
dismiss(animated: true, completion: nil)
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
Please anyone help me to solve it out.
Thanks in advance.
The only API provided by GMSAutocompleteViewController is to set the GMSCoordinateBounds like so (reference):
func placeAutocomplete() {
let visibleRegion = mapView.projection.visibleRegion()
let bounds = GMSCoordinateBounds(coordinate: visibleRegion.farLeft, coordinate: visibleRegion.nearRight)
let autocompleteController = GMSAutocompleteViewController()
acController.autocompleteBounds = bounds
autocompleteController.delegate = self
present(autocompleteController, animated: true, completion: nil)
}
I'm using place picker to pick a location on a map. After that, I want to get a GMSPlace list of restaurants in a specific radius, centered around the picked location.
Is there any way to implement that using the Google iOS API?
#IBAction func pickPlace(_ sender: UIButton) {
let config = GMSPlacePickerConfig(viewport: nil)
let placePicker = GMSPlacePickerViewController(config: config)
placePicker.delegate = self
present(placePicker, animated: true, completion: nil)
}
func placePicker(_ viewController: GMSPlacePickerViewController, didPick place: GMSPlace){
self.pickedNameLabel.text = place.name
self.pickedAddressLabel.text = place.formattedAddress?.components(separatedBy: ", ").joined(separator: "\n")
viewController.dismiss(animated: true, completion: nil)
}
func placePickerDidCancel(_ viewController: GMSPlacePickerViewController){
viewController.dismiss(animated: true, completion: nil)
}
In didPick delegate fetch the place lat long and pass it to :
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670,151.1957&radius=500&types=food&name=cruise&key=YOUR_API_KEY
Source:
https://developers.google.com/places/web-service/
I'm trying to prompt the user to create a new contact and pass in information. (specifically a phone and email)
I've found numerous examples of using a CNMutableContact and adding an email to it. However, any of the code involving the CNContact gives me a "Use of undeclared type" error.
How can I setup my class to prompt the user to save the contact?
import ContactsUI
//add CNContactViewControllerDelegate to your ViewController
class ViewController: UIViewController , CNContactViewControllerDelegate {
func addPhoneNumber(phNo : String) {
if #available(iOS 9.0, *) {
let store = CNContactStore()
let contact = CNMutableContact()
let homePhone = CNLabeledValue(label: CNLabelHome, value: CNPhoneNumber(stringValue :phNo ))
contact.phoneNumbers = [homePhone]
let controller = CNContactViewController(forUnknownContact : contact)
controller.contactStore = store
controller.delegate = self
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController!.pushViewController(controller, animated: true)
}
}
You Can Do Something Like This.
extension ViewController: CNContactViewControllerDelegate {
func showNewContactViewController() {
let contactViewController: CNContactViewController = CNContactViewController(forNewContact: nil)
contactViewController.contactStore = CNContactStore()
contactViewController.delegate = self
let navigationController: UINavigationController = UINavigationController(rootViewController: contactViewController)
present(navigationController, animated: false) {
print("Present")
}
}
}
Swift 4
import ContactsUI
implement delegate CNContactViewControllerDelegate
#IBAction func UserTap_Handler(_ sender: Any) {
self.navigationController?.isNavigationBarHidden = false
let con = CNContact()
let vc = CNContactViewController(forNewContact: con)
vc.delegate = self
_ = self.navigationController?.pushViewController(vc, animated: true)
}
//MARK:- contacts delegates
func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
print("dismiss contact")
self.navigationController?.popViewController(animated: true)
}
func contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
return true
}
I am using iOS 9 and Swift 2.2
I have implemented iOS inbuilt CNContactPickerViewController using CNContactPickerDelegate to get the contact numbers,
In the CNContactPickerViewController Screen, when I click on search field on top and search for a name, I need to add that name to my selection but nothing happens after tapping the contact.
I searched everywhere and dint find any solution to this
Do I need to add anything to my code or is it a iOS 9 bug
#IBAction func AddBtnKlkFnc(sender: AnyObject)
{
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.displayedPropertyKeys =
[CNContactPhoneNumbersKey]
self.presentViewController(contactPicker, animated: true, completion: nil)
}
func contactPicker(picker: CNContactPickerViewController, didSelectContacts ContctAryVar: [CNContact])
{
for ContctVar in ContctAryVar
{
let ContctDtlVar = ContctDtlCls()
ContctDtlVar.ManNamVar = CNContactFormatter.stringFromContact(ContctVar, style: .FullName)!
for ContctNumVar: CNLabeledValue in ContctVar.phoneNumbers
{
var MobNumVar = ((ContctNumVar.value as! CNPhoneNumber).valueForKey("digits") as? String)!
if(MobNumVar.Len() > 10)
{
MobNumVar = MobNumVar.GetLstSubSrgFnc(10)
}
ContctDtlVar.MobNumVar = MobNumVar
ContctDtlAryVar.append(ContctDtlVar)
}
}
}
The search results seem to be working in single selection mode only, so make sure you implement
func contactPicker(CNContactPickerViewController, didSelect: CNContact)
only, but not
func contactPicker(CNContactPickerViewController, didSelect: [CNContact])
If you implement both, the version wich takes only one CNContact as argument is ignored and the multi selection mode is used instead.
Use this updated code and
#IBAction func AddBtnKlkFnc(sender: AnyObject)
{
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.displayedPropertyKeys =
[CNContactPhoneNumbersKey]
self.presentViewController(contactPicker, animated: true, completion: nil)
}
func contactPicker(picker: CNContactPickerViewController, didSelectContacts ContctAryVar: [CNContact])
{
for ContctVar in ContctAryVar
{
let ContctDtlVar = ContctDtlCls()
ContctDtlVar.ManNamVar = CNContactFormatter.stringFromContact(ContctVar, style: .FullName)!
for ContctNumVar: CNLabeledValue in ContctVar.phoneNumbers
{
var MobNumVar = ((ContctNumVar.value as! CNPhoneNumber).valueForKey("digits") as? String)!
if(MobNumVar.Len() > 10)
{
MobNumVar = MobNumVar.GetLstSubSrgFnc(10)
}
ContctDtlVar.MobNumVar = MobNumVar
ContctDtlAryVar.append(ContctDtlVar)
}
}
delegate.didFetchContacts([contact])
navigationController?.popViewControllerAnimated(true)
}
Here is a swift 4 version
#IBAction func addPhoneContact(_ sender: UIButton) {
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.displayedPropertyKeys =
[CNContactPhoneNumbersKey]
self.present(contactPicker, animated: true, completion: nil)
}
extension ViewController: CNContactPickerDelegate {
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
picker.dismiss(animated: true, completion: nil)
let name = CNContactFormatter.string(from: contact, style: .fullName)
for number in contact.phoneNumbers {
let mobile = number.value.value(forKey: "digits") as? String
if (mobile?.count)! > 7 {
// your code goes here
}
}
}
}
Multi selection and search are mutually exclusive. If you want search to be working you have to go with single selection only and implement only single selection delegate method.