adamcmoore / nova-gmap
使用Google Maps提供纬度/经度数据的Laravel Nova字段。
v2.0.1
2021-09-17 11:58 UTC
Requires
- php: >=7.1.0
This package is auto-updated.
Last update: 2024-09-17 19:41:44 UTC
README
一个Nova字段控件,用于使用Google Maps更新纬度/经度位置。
安装
使用以下命令通过composer安装包:
composer require adamcmoore/nova-gmap
然后使用以下命令发布配置文件:
php artisan vendor:publish --provider="Acm\NovaGmap\FieldServiceProvider.php" --tag="config"
这将创建一个位于config/nova-gmaps.php
的配置文件,您需要在其中输入您的Google Maps API密钥、默认位置和缩放级别。
版本
- v1.0 Laravel 6 & 7
- v2.0 Laravel 8
用法
要将此字段添加到Nova资源中
NovaGmap::make('Location')
字段期望以对象的形式提供数据,包含latitude
和longitude
键,并将以相同格式返回请求中的数据。使用访问器和修改器来处理这些数据。
一个示例模型
class Shop extends Model { $fillable = [ 'title', 'description', 'address', 'location_lat', 'location_lng', ]; /* Provide the Location value to the Nova field */ public function getLocationAttribute() { return (object) [ 'latitude' => $this->location_lat, 'longitude' => $this->location_lng, ]; } /* Transform the returned value from the Nova field */ public function setLocationAttribute($value) { $location_lat = round(object_get($value, 'latitude'), 7); $location_lng = round(object_get($value, 'longitude'), 7); $this->attributes['location_lat'] = $location_lat; $this->attributes['location_lng'] = $location_lng; } }