Skip to content

Commit

Permalink
ability to set initial location
Browse files Browse the repository at this point in the history
  • Loading branch information
almassapargali committed Jul 29, 2015
1 parent 4614ba8 commit 6890881
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
28 changes: 20 additions & 8 deletions LocationPicker/LocationPicker/LocationPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public class LocationPickerViewController: UIViewController {

public var location: Location? {
didSet {
searchBar.text = flatMap(location, { $0.title }) ?? ""
updateAnnotation()
if isViewLoaded() {
searchBar.text = flatMap(location, { $0.title }) ?? ""
updateAnnotation()
}
}
}

Expand Down Expand Up @@ -72,6 +74,12 @@ public class LocationPickerViewController: UIViewController {
// search
navigationItem.titleView = searchBar
definesPresentationContext = true

if let location = location {
// present initial location if any
self.location = location
showSelectedLocation()
}
}

public override func viewWillDisappear(animated: Bool) {
Expand Down Expand Up @@ -111,6 +119,15 @@ public class LocationPickerViewController: UIViewController {
mapView.removeAnnotations(annotations)
}
}

func showSelectedLocation() {
if let location = location {
// change review to center result location
let region = MKCoordinateRegionMakeWithDistance(location.coordinate,
resultRegionDistance, resultRegionDistance)
mapView.setRegion(region, animated: true)
}
}
}

// MARK: Searching
Expand Down Expand Up @@ -154,14 +171,9 @@ extension LocationPickerViewController: UISearchResultsUpdating {
func selectedLocation(location: Location) {
// dismiss search results
dismissViewControllerAnimated(true) {

// change review to center result location
let region = MKCoordinateRegionMakeWithDistance(location.coordinate,
self.resultRegionDistance, self.resultRegionDistance)
self.mapView.setRegion(region, animated: true)

// set location, this also adds annotation
self.location = location
self.showSelectedLocation()
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions LocationPickerDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ import LocationPicker

class ViewController: UIViewController {
@IBOutlet weak var locationNameLabel: UILabel!
var location: Location?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "LocationPicker" {
let locationPicker = segue.destinationViewController as! LocationPickerViewController
locationPicker.location = location

locationPicker.completion = { [weak self] location in
self?.location = location
self?.locationNameLabel.text = flatMap(location, { $0.title }) ?? "No location selected"
}
}
Expand Down

0 comments on commit 6890881

Please sign in to comment.