chiiya / nova-leaflet-field

Laravel Nova 的 Leaflet 地图字段

1.7.0 2021-12-16 12:54 UTC

This package is auto-updated.

Last update: 2024-09-16 19:05:39 UTC


README

Nova Leaflet Field

🗺️ Laravel Nova 的 Leaflet 地图字段,带有地理编码搜索功能



索引

> Installation ..................................................................... 
> Usage ............................................................................ 

安装

composer require chiiya/nova-leaflet-field
# Publish marker icon assets
php artisan vendor:publish --provider="Chiiya\NovaLeafletField\FieldServiceProvider"

使用

要使用 leaflet 字段,只需指定一个标签名称

LeafletField::make(__('Geo-Location'))

该字段仅在详细和表单视图中显示。它将尝试在关联模型中查找 latitudelongitude 字段以在地图上设置初始标记,但您可以自定义此设置,如果您的字段名称不同

LeafletField::make(__('Geo-Location'))
    ->latitudeField('lat')
    ->longitudeField('lng')

Leaflet 的默认瓦片和搜索提供者是 OpenStreetMaps。您可以通过 leaflet-geosearch 配置任何支持的搜索提供者

LeafletField::make(__('Geo-Location'))
    ->searchProvider(SearchProvider::GOOGLE)
    ->searchProviderKey('api-key')
    ->searchProviderOptions(['language' => 'de', 'region' => 'de'])

要自定义瓦片,请提供瓦片 URL

LeafletField::make(__('Geo-Location'))
    ->tileUrl('https://{s}.tile.osm.org/{z}/{x}/{y}.png')

有许多选项可以自定义地图行为

LeafletField::make(__('Geo-Location'))
    // Make the map marker draggable
    ->draggable()
    // Customize the geo-search search label
    ->searchLabel(__('Enter address'))
    // Default map zoom
    ->zoom(12)
    // Customize default latitude & longitude
    ->defaultCoordinates(0.0, 0.0)

验证

当将字段标记为 required 时,该字段将验证是否已选择除了默认坐标以外的坐标。如果您希望禁用此行为,请调用以下方法

LeafletField::make(__('Geo-Location'))
    ->allowDefaultCoordinates()

您还可以自定义选择默认坐标时显示的错误消息

LeafletField::make(__('Geo-Location'))
    ->errorMessage(__('Please select a valid location'))

地址字段填充

当使用 Google 搜索提供者时,您可以使用所选位置的的数据来填充模型上的其他字段

LeafletField::make(__('Geo-Location'))
    ->searchProvider(SearchProvider::GOOGLE)
    ->searchProviderKey('api-key')
    ->populateAddress()
    ->populatePostalCode('zip')
    ->populateCity()
    ->populateCountry()

该字段将尝试在页面上查找具有默认(addresspostal_codecitycountry)或自定义名称的其他输入,并将从 Google 地理编码 API 收到的值填充它们。对于地址,您可以指定自定义格式

LeafletField::make(__('Geo-Location'))
    ->searchProvider(SearchProvider::GOOGLE)
    ->searchProviderKey('api-key')
    ->populateAddress()
    // Defaults to '{street_name} {street_number}'
    ->populatedAddressFormat('{street_number}, {street_name}')

此功能仅在使用 Google 搜索提供者时有效。