LocationPicker
LocationPickerViewController
是一个 UIViewController
子类,允许用户通过搜索或在地图上选择来选择位置。它旨在与 UIImagePickerController
类似。
用户可以通过搜索或在地图上长按来选择位置。在两种情况下,您都会收到 CLPlacemark,其中包含位置坐标以及国家、州、城市、街道地址和 POI 名称等信息。
安装
使用 Swift 5,对于 Swift 4.2 使用版本 1.3.0
,对于 Swift 3 使用 1.0.3
,对于 Swift 2 使用 0.6.0
。
Carthage
github "almassapargali/LocationPicker"
CocoaPods
pod 'LocationPicker'
截图
地图 | 搜索 | 选择 |
---|---|---|
![]() |
![]() |
![]() |
用法
在代码中创建一个新实例(LocationPickerViewController
)或在Storyboard中设置UIViewController
类。然后提供完成块,它将在用户关闭视图控制器时调用。
let locationPicker = LocationPickerViewController()
// you can optionally set initial location
let location = CLLocation(latitude: 35, longitude: 35)
let initialLocation = Location(name: "My home", location: location)
locationPicker.location = initialLocation
// button placed on right bottom corner
locationPicker.showCurrentLocationButton = true // default: true
// default: navigation bar's `barTintColor` or `UIColor.white`
locationPicker.currentLocationButtonBackground = .blue
// ignored if initial location is given, shows that location instead
locationPicker.showCurrentLocationInitially = true // default: true
locationPicker.mapType = .Standard // default: .Hybrid
// for searching, see `MKLocalSearchRequest`'s `region` property
locationPicker.useCurrentLocationAsHint = true // default: false
locationPicker.searchBarPlaceholder = "Search places" // default: "Search or enter an address"
locationPicker.searchHistoryLabel = "Previously searched" // default: "Search History"
// optional region distance to be used for creation region when user selects place from search results
locationPicker.resultRegionDistance = 500 // default: 600
locationPicker.completion = { location in
// do some awesome stuff with location
}
navigationController?.pushViewController(locationPicker, animated: true)
许可证
LocationPicker在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。