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

Ability to pick searched location without results #5

Merged
merged 1 commit into from
May 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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