rainlab / location-plugin
October CMS 位置插件
Requires
- php: ^8.0.2
- composer/installers: ~1.0
README
此插件为 October CMS 添加基于位置的特性。
- 轻松添加国家/地区到任何模型
- 地址查找表单小部件(Google API)
在 October CMS 市场查看此插件
扩展功能
要集成前端用户的位置,请考虑安装 RainLab.UserPlus
插件。
Google API 密钥要求
使用 Google Maps 服务需要 API 密钥。您可以从以下链接生成密钥:
复制密钥并在 设置 > 位置设置 区域中输入。如果您发现地址查找器无法正常工作,您可能需要启用 Places API 和 Maps JavaScript API。
将国家/地区添加到任何模型
此插件提供了一种简单的方法将位置字段、国家和州添加到任何模型中。只需将这些列添加到数据库表中
$table->bigInteger('country_id')->unsigned()->nullable()->index(); $table->bigInteger('state_id')->unsigned()->nullable()->index();
然后在模型类中实现 RainLab\Location\Traits\LocationModel 特性
use \RainLab\Location\Traits\LocationModel;
这将自动创建两个“属于”关系
- state - RainLab\Location\Models\State 的关系
- country - RainLab\Location\Models\Country 的关系
后端使用
表单
您可以根据需要添加以下表单字段定义
country: label: Country type: dropdown span: left placeholder: -- select country -- state: label: State type: dropdown span: right dependsOn: country placeholder: -- select state --
列表
对于列表列定义,您可以使用以下代码片段
country: label: Country searchable: true relation: country select: name sortable: false state: label: State searchable: true relation: state select: name sortable: false
前端使用
前端还可以通过渲染位置组件提供的 @form-select-country
和 @form-select-state
部分来使用这些关系。在继续之前,请确保您已将 location
组件附加到页面或布局。
<div class="form-group"> <label for="accountCountry">Country</label> {% partial '@form-select-country' countryId=user.country_id %} </div> <div class="form-group"> <label for="accountState">State</label> {% partial '@form-select-state' countryId=user.country_id stateId=user.state_id %} </div>
简码访问器
此行为还将添加一个特殊的简码访问器和设置器到模型中,将 country_code
和 state_code
转换为其相应的标识符。
// Softly looks up and sets the country_id and state_id // for these Country and State relations. $model->country_code = "US"; $model->state_code = "FL"; $model->save();
地址查找表单小部件
此插件引入了一个名为 addressfinder
的地址查找表单字段。表单小部件渲染一个 Google Maps 自动完成地址字段,该字段会根据在地址中输入和选择的内容自动填充映射字段。
可用的映射
- street
- city
- zip
- state
- country
- country-long
- latitude
- longitude
- vicinity
可用的选项
您可以通过定义 countryRestriction
选项来限制地址查找到某些国家。该选项接受一个以逗号分隔的 ISO 3166-1 ALPHA-2 兼容国家代码列表(见:https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)。
默认情况下,street
映射将门牌号码放在街道名称之前。然而,在某些国家,数字通常放在街道名称之后。您可以使用 reverseStreetNumber: true
选项来反转顺序。
使用
# =================================== # Form Field Definitions # =================================== fields: address: label: Address type: addressfinder countryRestriction: 'us,ch' reverseStreetNumber: false fieldMap: latitude: latitude longitude: longitude city: city zip: zip street: street country: country_code state: state_code vicinity: vicinity city: label: City zip: label: Zip street: label: Street country_code: label: Country state_code: label: State latitude: label: Latitude longitude: label: Longitude vicinity: label: Vicinity
许可协议
此插件是 October CMS 平台的官方扩展,如果您拥有平台许可,则可免费使用。有关详细信息,请参阅 EULA 许可协议。