Skip to content
This repository has been archived by the owner on Apr 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from v-ken/master
Browse files Browse the repository at this point in the history
Ability to pick searched location without results
  • Loading branch information
Jerome Tan authored and Jerome Tan committed May 8, 2016
2 parents 16b133e + 3ed0afc commit e94b4b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
6 changes: 5 additions & 1 deletion LocationPicker/LocationItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ public class LocationItem: NSObject, NSCoding {

public override var hashValue: Int {
get {
return "\(coordinate.latitude), \(coordinate.longitude)".hashValue
if CLLocationCoordinate2DIsValid(coordinateObjectFromTuple(coordinate)) {
return "\(coordinate.latitude), \(coordinate.longitude)".hashValue
} else {
return mapItem.name?.hashValue ?? "".hashValue
}
}
}

Expand Down
19 changes: 15 additions & 4 deletions LocationPicker/LocationPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -756,9 +756,16 @@ public class LocationPicker: UIViewController, UISearchBarDelegate, UITableViewD
localSearchRequest.region = MKCoordinateRegionMakeWithDistance(currentCoordinate, searchDistance, searchDistance)
}
MKLocalSearch(request: localSearchRequest).startWithCompletionHandler({ (localSearchResponse, error) -> Void in
guard error == nil else { return }
guard let localSearchResponse = localSearchResponse else { return }
guard localSearchResponse.mapItems.count > 0 else { return }
guard error == nil,
let localSearchResponse = localSearchResponse where localSearchResponse.mapItems.count > 0 else {
// Create map item with name and invalid placemark coordinate (since placemark is not optional in MKMapItem)
let placemark = MKPlacemark(coordinate: kCLLocationCoordinate2DInvalid, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = searchText
self.searchResultLocations = [LocationItem(mapItem: mapItem)]
self.tableView.reloadData()
return
}

self.searchResultLocations = localSearchResponse.mapItems.map({ LocationItem(mapItem: $0) })
self.tableView.reloadData()
Expand Down Expand Up @@ -942,7 +949,11 @@ public class LocationPicker: UIViewController, UISearchBarDelegate, UITableViewD
selectedLocationItem = locationItem
searchBar.text = locationItem.name
let coordinate = coordinateObjectFromTuple(locationItem.coordinate)
showMapViewWithCenterCoordinate(coordinate, WithDistance: longitudinalDistance)
if CLLocationCoordinate2DIsValid(coordinate) {
showMapViewWithCenterCoordinate(coordinate, WithDistance: longitudinalDistance)
} else {
closeMapView()
}

doneButtonItem?.enabled = true
locationDidSelect(locationItem)
Expand Down

0 comments on commit e94b4b6

Please sign in to comment.