From 8bf1451581891f715de7845c17d3a30d6d4b2015 Mon Sep 17 00:00:00 2001 From: Piotr Bernad Date: Tue, 9 Jul 2019 13:25:57 +0200 Subject: [PATCH] Added Initial coordinate and initial zoom --- PlacesPicker/Classes/PlacePickerConfig.swift | 8 +++++++- PlacesPicker/Classes/PlacePickerController.swift | 8 ++++++++ PlacesPicker/Classes/PlacesDataSource.swift | 6 ++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/PlacesPicker/Classes/PlacePickerConfig.swift b/PlacesPicker/Classes/PlacePickerConfig.swift index 215c894..3b29b83 100644 --- a/PlacesPicker/Classes/PlacePickerConfig.swift +++ b/PlacesPicker/Classes/PlacePickerConfig.swift @@ -14,6 +14,8 @@ public struct PlacePickerConfig { public let placeFields: GMSPlaceField public let pickerRenderer: PickerRenderer public let placesFilter: GMSAutocompleteFilter? + public let initialCoordinate: CLLocationCoordinate2D? + public let initialZoom: Float public static var `default`: PlacePickerConfig { return PlacePickerConfig() @@ -22,11 +24,15 @@ public struct PlacePickerConfig { public init(listRenderer: PlacesListRenderer = DefaultPlacesListRenderer(), placeFields: GMSPlaceField = GMSPlaceField.defaultFields, placesFilter: GMSAutocompleteFilter? = nil, - pickerRenderer: PickerRenderer = DefaultPickerRenderer()) { + pickerRenderer: PickerRenderer = DefaultPickerRenderer(), + initialCoordinate: CLLocationCoordinate2D? = nil, + initialZoom: Float = 13.0) { self.listRenderer = listRenderer self.placeFields = placeFields self.placesFilter = placesFilter self.pickerRenderer = pickerRenderer + self.initialZoom = initialZoom + self.initialCoordinate = initialCoordinate } } diff --git a/PlacesPicker/Classes/PlacePickerController.swift b/PlacesPicker/Classes/PlacePickerController.swift index ab59da2..11dbecb 100644 --- a/PlacesPicker/Classes/PlacePickerController.swift +++ b/PlacesPicker/Classes/PlacePickerController.swift @@ -29,6 +29,14 @@ public class PlacePickerController: UIViewController, PlacesDataSourceDelegate { public override func viewDidLoad() { super.viewDidLoad() setupNavigationBar() + setInitialCoordinateIfNeeded() + } + + private func setInitialCoordinateIfNeeded() { + if let initialCoordinate = self.config.initialCoordinate { + let position = GMSCameraPosition(latitude: initialCoordinate.latitude, longitude: initialCoordinate.longitude, zoom: config.initialZoom) + pickerView.mapView.animate(to: position) + } } // Internal diff --git a/PlacesPicker/Classes/PlacesDataSource.swift b/PlacesPicker/Classes/PlacesDataSource.swift index 23143a7..4e7f3c1 100644 --- a/PlacesPicker/Classes/PlacesDataSource.swift +++ b/PlacesPicker/Classes/PlacesDataSource.swift @@ -132,8 +132,10 @@ extension PlacesDataSource: UITableViewDataSource { extension PlacesDataSource: GMSAutocompleteViewControllerDelegate { public func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) { self.state = .singlePlace(place: place) - self.delegate?.autoCompleteControllerDidProvide(place: place) - viewController.dismiss(animated: true, completion: nil) + + viewController.dismiss(animated: true, completion: { [weak self] in + self?.delegate?.autoCompleteControllerDidProvide(place: place) + }) } public func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {