Introduction
LocationPickerViewController is a customizable and easy-to-use location picker view controller written in Swift. It allows users to easily pick a location by searching for places or selecting a location on the map.
Key Features
- Search places by name, address, or keyword
- Filter search results by location bounds or bias
- Display search results as a list
- Select a location by tapping on the map
- Customizable appearance and behavior
Installation
To install LocationPickerViewController using CocoaPods, add the following line to your Podfile:
pod 'LocationPickerViewController'
Usage
To use LocationPickerViewController, follow these steps:
- Import the module in your view controller:
- Instantiate a LocationPickerViewController:
- Set the delegate (optional):
- Present the view controller:
// Swift
import LocationPickerViewController
// Objective-C
@import LocationPickerViewController;
// Swift
let locationPicker = LocationPickerViewController()
// Objective-C
LocationPickerViewController *locationPicker = [[LocationPickerViewController alloc] init];
// Swift
locationPicker.delegate = self
// Objective-C
locationPicker.delegate = self;
// Swift
present(locationPicker, animated: true, completion: nil)
// Objective-C
[self presentViewController:locationPicker animated:YES completion:nil];
Delegate Methods
LocationPickerViewController provides delegate methods to handle user actions. Conform to the LocationPickerDelegate protocol and implement the necessary methods:
// Swift
func locationPicker(_ locationPicker: LocationPickerViewController, didSelectLocation location: Location) {
// Handle when a location is selected
}
func locationPickerDidCancel(_ locationPicker: LocationPickerViewController) {
// Handle when the picker is canceled
}
// Objective-C
- (void)locationPicker:(LocationPickerViewController *)locationPicker didSelectLocation:(Location *)location {
// Handle when a location is selected
}
- (void)locationPickerDidCancel:(LocationPickerViewController *)locationPicker {
// Handle when the picker is canceled
}
Customization
You can customize the appearance and behavior of LocationPickerViewController using various properties and methods:
- Customize the search bar:
// Swift
locationPicker.searchBarPlaceholder = "Search for places"
locationPicker.tintColor = .red
// Objective-C
locationPicker.searchBarPlaceholder = @"Search for places";
locationPicker.tintColor = [UIColor redColor];
// Swift
let initialLocation = CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194)
let span = MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
locationPicker.initialRegion = MKCoordinateRegion(center: initialLocation, span: span)
// Objective-C
CLLocationCoordinate2D initialLocation = CLLocationCoordinate2DMake(37.7749, -122.4194);
MKCoordinateSpan span = MKCoordinateSpanMake(0.2, 0.2);
locationPicker.initialRegion = MKCoordinateRegionMake(initialLocation, span);
// Swift
locationPicker.searchQueryLocationBias = CLLocation(latitude: 37.7749, longitude: -122.4194)
locationPicker.searchQueryLocationBounds = MKCoordinateRegion(center: initialLocation, span: span).asCoordinateBounds()
// Objective-C
locationPicker.searchQueryLocationBias = [[CLLocation alloc] initWithLatitude:37.7749 longitude:-122.4194];
locationPicker.searchQueryLocationBounds = [[MKCoordinateRegionMake(initialLocation, span)] asCoordinateBounds];
// Swift
locationPicker.pinTintColor = .blue
// Objective-C
locationPicker.pinTintColor = [UIColor blueColor];
Requirements
- iOS 11.0+
- Swift 4.2+
Author
LocationPickerViewController is developed and maintained by XYZ Company.
License
LocationPickerViewController is available under the MIT license. See the LICENSE file for more info.