Swift: Google Place Picker, Avoiding the Map UI - ios

In my Swift iOS App, I want to take the user to place autocomplete search screen and let them search in context to their current location. Apparently this is not possible with Google Place Autocomplete since there is no way to pass current location context to it.
My second choice is to use Google Place Picker's search screen because when I start Place Picker centred on current location and then tap search, it searches places in context with current location.
My question is, is it possible to take the user directly to Place Picker's search screen and then dismiss the Place Picker after grabbing the picked place information, avoid the main UI of Place Picker?

It's a little confusing in the docs, but I think what you want is to use the GMSAutocompleteViewController instead of the place picker.
Sample code below, links to the docs here.
import UIKit
import GooglePlaces
class ViewController: UIViewController {
// Present the Autocomplete view controller when the button is pressed.
#IBAction func autocompleteClicked(_ sender: UIButton) {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
present(autocompleteController, animated: true, completion: nil)
}
}
extension ViewController: GMSAutocompleteViewControllerDelegate {
// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(place.name)")
print("Place address: \(place.formattedAddress)")
print("Place attributions: \(place.attributions)")
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
}
}

Related

How to get the google places automatically

I want display the Google Automatic places in Text field.
I write the following code but I unable to understand where the I give apikey.
Same time I getting the latitude and longitude also for selected address.
import UIKit
import GoogleMaps
import GooglePlaces
class ViewController: UIViewController ,UITextFieldDelegate,GMSAutocompleteViewControllerDelegate{
#IBOutlet weak var placeaddress: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let apikey = "API_KEY"
self.placeaddress.delegate = self
}
func textFieldDidBeginEditing(_ textField: UITextField) {
let acController = GMSAutocompleteViewController()
acController.delegate = self
self.present(acController, animated: true, completion: nil)
}
// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(place.name)")
print("Place address: \(String(describing: place.formattedAddress))")
print("Place attributions: \(String(describing: place.attributions))")
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)
}
}
First import Google Framework into appDelegate
import GoogleMaps
After that provide your api key into didFinishLaunchingWithOptions in appDelegate
GMSServices.provideAPIKey("Your API Key")
Hope this will help you.

Swift google autocomplete with local search

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)
}

Google Maps/PlacePicker ios api - is there a way to get nearby places as a GMSplace list?

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/

MessageComposeViewController only presents once

I'm using Xcode 8 to build an application for iPhone.My simple app has an button. When user tap on this button, messageComposeViewController is called and with phone number and message content filled.The message went through successfully when I clicked on Send button.The problem is MessageComposeViewController is showing only once.After the message sent, when I tapped on the button to call it, a black screen showed up instead of the message composer.My code is attached below. I appreciate any help.
import UIKit
import MessageUI
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
let msg = MFMessageComposeViewController()
#IBOutlet weak var coordinate_label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func sendMessage(_ sender: AnyObject) {
self.msg.body = "Message Content"
self.msg.recipients = ["xxx-xxx-xxxx"]
self.msg.messageComposeDelegate = self
self.present(msg, animated: false, completion: nil)
}
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
switch result.rawValue {
case MessageComposeResult.cancelled.rawValue:
dismiss(animated: true, completion: nil)
case MessageComposeResult.failed.rawValue:
dismiss(animated: true, completion: nil)
case MessageComposeResult.sent.rawValue:
dismiss(animated: true, completion: nil)
default:
break;
}
}
}
Try this code
import Foundation
import MessageUI
let textMessageRecipients = ["1-800-867-5309"] // for pre-populating the recipients list (optional, depending on your needs)
class MessageComposer: NSObject, MFMessageComposeViewControllerDelegate {
// A wrapper function to indicate whether or not a text message can be sent from the user's device
func canSendText() -> Bool {
return MFMessageComposeViewController.canSendText()
}
// Configures and returns a MFMessageComposeViewController instance
func configuredMessageComposeViewController() -> MFMessageComposeViewController {
let messageComposeVC = MFMessageComposeViewController()
messageComposeVC.messageComposeDelegate = self // Make sure to set this property to self, so that the controller can be dismissed!
messageComposeVC.recipients = textMessageRecipients
messageComposeVC.body = "Hey friend - Just sending a text message in-app using Swift!"
return messageComposeVC
}
// MFMessageComposeViewControllerDelegate callback - dismisses the view controller when the user is finished with it
func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
}
Note, latest syntax 2017 ..
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
controller.dismiss(animated: true, completion: nil)
}
In Your viewController ..
import UIKit
class ViewController: UIViewController {
// Create a MessageComposer
let messageComposer = MessageComposer()
#IBAction func sendTextMessageButtonTapped(sender: UIButton) {
// Make sure the device can send text messages
if (messageComposer.canSendText()) {
// Obtain a configured MFMessageComposeViewController
let messageComposeVC = messageComposer.configuredMessageComposeViewController()
// Present the configured MFMessageComposeViewController instance
// Note that the dismissal of the VC will be handled by the messageComposer instance,
// since it implements the appropriate delegate call-back
presentViewController(messageComposeVC, animated: true, completion: nil)
} else {
// Let the user know if his/her device isn't able to send text messages
let errorAlert = UIAlertView(title: "Cannot Send Text Message", message: "Your device is not able to send text messages.", delegate: self, cancelButtonTitle: "OK")
errorAlert.show()
}
}
}

Adding and Saving New Contact to AddressBook

I have a problem that I can't quite seem to solve even though I've used resources I've found on Google and on here. I've only started being taught Swift and how to use Xcode about a month ago, hence, I'm very new, and the problem I have is probably very simple to others.
What I'm trying to do is add and save a new contact to the addressbook. I can get from the app to the Contacts app no problem, it's just I can't save the new contact information.
import AddressBookUI
import AddressBook
class ViewController: UIViewController {
#IBOutlet weak var contactLink: UIButton!
#IBAction func contactLink(sender: AnyObject) {
var viewController: ABNewPersonViewController = ABNewPersonViewController()
self.presentViewController(viewController, animated: true, completion: nil)
}
}
Here's the code I'm using that relates to my problem. Any assistance will be appreciated.
Nowadays, you'd use ContactsUI framework. So, in Swift 3, you could do:
import ContactsUI
class ViewController: UIViewController, CNContactViewControllerDelegate {
#IBAction func contactLink(_ sender: AnyObject) {
let controller = CNContactViewController(forNewContact: nil)
controller.delegate = self
let navigationController = UINavigationController(rootViewController: controller)
self.present(navigationController, animated: true)
}
func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
viewController.navigationController?.dismiss(animated: true)
}
}
My original answer, using AddressBookUI framework in Swift 2 is below.
The Swift code is:
import AddressBookUI
class ViewController: UIViewController, ABNewPersonViewControllerDelegate {
#IBAction func contactLink(sender: AnyObject) {
let controller = ABNewPersonViewController()
controller.newPersonViewDelegate = self
let navigationController = UINavigationController(rootViewController: controller)
self.presentViewController(navigationController, animated: true, completion: nil)
}
func newPersonViewController(newPersonView: ABNewPersonViewController!, didCompleteWithNewPerson person: ABRecord!) {
newPersonView.navigationController?.dismissViewControllerAnimated(true, completion: nil);
}
}
See the Prompting the User to Create a New Person Record section of the Address Book Programming Guide: User Interaction: Prompting for and Displaying Data.

Resources