on IOS, Looking for option to detect current locat

2019-08-10 03:51发布

I dont have familiarity with Swift3, however i am getting my app developed from freelancers, but they said, they are not not able to add detect current location in the google place api sdk implementation in my IOS app developing in swift3 to search the address. I am trying to implement my address search page like below

current location link should auto detect user's current location like on image

Any pointer in this functionality will be a great help. Please share any example/tutorial of such implementation which I can share with developer to implement.

1条回答
相关推荐>>
2楼-- · 2019-08-10 04:37

first install

pod 'GooglePlaces'
pod 'GooglePlacePicker'
pod 'GoogleMaps'

Then follow the below code just for over view

import GoogleMaps
import GooglePlaces

    class SrarchPlacesViewController: UIViewController, UISearchDisplayDelegate,UISearchBarDelegate {

        @IBOutlet weak var searchView: UISearchBar!
        var searchBar: UISearchBar?
        var tableDataSource: GMSAutocompleteTableDataSource?
        var srchDisplayController: UISearchDisplayController?
        var resultsViewController: GMSAutocompleteResultsViewController?
        var searchController: UISearchController?

     override func viewDidLoad() {
            super.viewDidLoad()
            searchView.delegate = self
            searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: 250.0, height: 44.0))

            tableDataSource = GMSAutocompleteTableDataSource()
            tableDataSource?.delegate = self


            srchDisplayController = UISearchDisplayController(searchBar: searchView, contentsController: self)
            srchDisplayController?.searchResultsDataSource = tableDataSource
            srchDisplayController?.searchResultsDelegate = tableDataSource

            srchDisplayController?.delegate = self


        }
       @objc(didUpdateAutocompletePredictionsForTableDataSource:) func didUpdateAutocompletePredictions(for tableDataSource: GMSAutocompleteTableDataSource) {
        // Turn the network activity indicator off.
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
        // Reload table data.
        srchDisplayController?.searchResultsTableView.reloadData()
    }

    @objc(didRequestAutocompletePredictionsForTableDataSource:) func didRequestAutocompletePredictions(for tableDataSource: GMSAutocompleteTableDataSource) {
        // Turn the network activity indicator on.
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
        // Reload table data.
        srchDisplayController?.searchResultsTableView.reloadData()
    }

    //MARK:- SEARCH BAR FUCNTION

    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        // self.tableViews.hidden = true
        dPrint("BIGIN")
    }

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        // self.tableViews.hidden = false
        dPrint("ending")

    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        // self.tableViews.hidden = false
        dPrint("cancel")
    }



    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if(searchText == ""){
            dPrint("text chnange")
            // self.tableViews.hidden = false
        }

    }

    @objc(tableDataSource:didSelectPrediction:) func tableDataSource(_ tableDataSource: GMSAutocompleteTableDataSource, didSelect prediction: GMSAutocompletePrediction) -> Bool {
        return true
    }


    }
    extension SrarchPlacesViewController: GMSAutocompleteTableDataSourceDelegate{
        /**
         * Called when a non-retryable error occurred when retrieving autocomplete predictions or place
         * details. A non-retryable error is defined as one that is unlikely to be fixed by immediately
         * retrying the operation.
         * <p>
         * Only the following values of |GMSPlacesErrorCode| are retryable:
         * <ul>
         * <li>kGMSPlacesNetworkError
         * <li>kGMSPlacesServerError
         * <li>kGMSPlacesInternalError
         * </ul>
         * All other error codes are non-retryable.
         * @param tableDataSource The |GMSAutocompleteTableDataSource| that generated the event.
         * @param error The |NSError| that was returned.
         */
        public func tableDataSource(_ tableDataSource: GMSAutocompleteTableDataSource, didFailAutocompleteWithError error: Error) {
            dPrint("Error: \(error.localizedDescription)")
        }

        func tableDataSource(_ tableDataSource: GMSAutocompleteTableDataSource, didAutocompleteWith place: GMSPlace) {
            srchDisplayController?.isActive = false

            // Do something with the selected place.
           print(place.coordinate)

            print("Place name: \(place.name)")
            print("Place address: \(place.formattedAddress)")
            print("Place attributions: \(place.attributions)")
        }

        @objc(searchDisplayController:shouldReloadTableForSearchString:) func searchDisplayController(_ controller: UISearchDisplayController, shouldReloadTableForSearch searchString: String?) -> Bool {
            tableDataSource?.sourceTextHasChanged(searchString)
            return false
        }


    }
查看更多
登录 后发表回答