LocationRadiusPicker

概述

Location Radius Picker(位置半径选择器)是一个开源的 UIViewController 子类,允许用户在地图上选择一个特定位置并定义一个围绕它的半径。该选择器高度可定制,并集成了位置搜索功能,使其非常适合需要基于位置功能的应用。

用例示例

当使用 UNLocationNotificationTrigger 安排基于位置的提醒时,可以使用此选择器来选择一个地理区域,以便在用户到达/离开时收到提醒。

特性

系统要求

安装

您可以使用以下任一方法集成 Location Radius Picker

  1. Swift Package Manager
dependencies: [
  // ...
  .package(url: "https://github.com/birkoof/LocationRadiusPicker.git"),
],
  1. 手动安装

使用示例

使用 Location Radius Picker 非常简单。首先,使用 LocationRadiusPickerConfigrationBuilder 构建配置,并将其传递给初始化器。您可以选择呈现或推送视图控制器。

注意:如果您选择推送视图控制器,您可以通过在构建器上调用 overrideNavigationBarAppearance(false) 来禁用导航栏外观覆盖。 这确保了选择器具有与推送它的视图控制器相同的导航栏外观。

注意:如果您选择呈现选择器,请务必使用 UINavigationController 包装选择器。

let configuration = LocationRadiusPickerConfigurationBuilder(initialRadius: 100, minimumRadius: 50, maximumRadius: 2000)
        .title("Location Radius Picker")
        .navigationBarSaveButtonTitle("Save")
        .showNavigationBarSaveButton(true)
        .cancelButtonTitle("Cancel")
        .initialLocation(LocationCoordinates(latitude: 37.331711, longitude: -122.030773))
        .radiusBorderColor(.systemBlue)
        .radiusBorderWidth(3)
        .radiusColor(.systemBlue.withAlphaComponent(0.3))
        .radiusLabelColor(.label)
        .grabberColor(.systemBlue)
        .grabberSize(20)
        .unitSystem(.system)
        .vibrateOnResize(true)
        .circlePadding(17)
        .overrideNavigationBarAppearance(true)
        .mapPinImage(UIImage(resource: .mapPin))
        .calloutSelectButtonText("Select")
        .calloutSelectButtonTextColor(.systemBlue)
        .showSaveButton(true)
        .saveButtonTitle("Select location")
        .saveButtonBackgroundColor(.systemBlue)
        .saveButtonTextColor(.white)
        .saveButtonCornerStyle(.capsule)
        .searchFunctionality(true)
        .showSearchHistory(true)
        .historyHeaderText("Previously searched")
        .searchBarPlaceholder("Search or Enter Address")
        .build()

let picker = LocationRadiusPickerController(configuration: configuration) { result in
        let radius = result.radius
        let locationName = result.location.name
        let locationAddress = result.location.address
        let locationCenterLatitude = result.location.coordinates.latitude
        let locationCenterLongitude = result.location.coordinates.longitude
    
        // ... do something with the result
}

// if presenting, make sure to wrap the picker with a UINavigationController
present(UINavigationController(rootViewController: picker), animated: true)

除了 initial(初始)minimum(最小)maximum radius(最大半径) 值(必须提供)之外,所有配置选项都是可选的。

注意:如果初始半径超出指定范围(介于最小值和最大值之间),它将自动调整以适应有效范围。

有关完整功能的示例,请查看此存储库中包含的 demo app(演示应用程序)

许可证

LocationRadiusPickerMIT License 许可下发布。