From d207749eb4d4eb939e65abf2f7a0b3ca10ce7d34 Mon Sep 17 00:00:00 2001 From: z-song Date: Mon, 7 Jan 2019 23:35:28 +0800 Subject: [PATCH] first commit --- .gitignore | 7 ++ LICENSE | 20 ++++++ README.md | 73 +++++++++++++++++++ composer.json | 33 +++++++++ resources/views/latlong.blade.php | 25 +++++++ src/Extension.php | 12 ++++ src/Latlong.php | 112 ++++++++++++++++++++++++++++++ src/LatlongServiceProvider.php | 26 +++++++ src/Map/AbstractMap.php | 36 ++++++++++ src/Map/Amap.php | 62 +++++++++++++++++ src/Map/Baidu.php | 61 ++++++++++++++++ src/Map/Google.php | 76 ++++++++++++++++++++ src/Map/Tencent.php | 63 +++++++++++++++++ src/Map/Yandex.php | 50 +++++++++++++ 14 files changed, 656 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 resources/views/latlong.blade.php create mode 100644 src/Extension.php create mode 100644 src/Latlong.php create mode 100644 src/LatlongServiceProvider.php create mode 100644 src/Map/AbstractMap.php create mode 100644 src/Map/Amap.php create mode 100644 src/Map/Baidu.php create mode 100644 src/Map/Google.php create mode 100644 src/Map/Tencent.php create mode 100644 src/Map/Yandex.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9d4b362 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.DS_Store +phpunit.phar +/vendor +composer.phar +composer.lock +*.project +.idea/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..229071a --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2015 Jens Segers + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..caf21a8 --- /dev/null +++ b/README.md @@ -0,0 +1,73 @@ +经纬度选择器 +====== + +这个扩展用来帮助你在form表单中选择经纬度,用来替代`Laravel-admin`中内置的`Form\Field\Map`组件, 组件支持的地图包括`Google map`、`百度地图`、`高德地图`、`腾讯地图`、`Yadex map`. + +## Installation + +```bash +composer require laravel-admin-ext/latlong -vvv +``` + +## Configuration + +打开config/admin.php,按照你的情况在extensions部分加上如下的配置: + +```php + + 'extensions' => [ + + 'latlong' => [ + + // 是否开始这个组件,默认true + 'enable' => true, + + // 选择下面指定的provider + 'default' => 'yandex', + + // 根据上面的选择,填写相应地图的api_key,api_key需要到相应的平台去自行申请 + 'providers' => [ + + 'google' => [ + 'api_key' => '', + ], + + 'baidu' => [ + 'api_key' => 'xck5u2lga9n1bZkiaXIHtMufWXQnVhdx', + ], + + 'tencent' => [ + 'api_key' => 'VVYBZ-HRJCX-NOJ4Z-ZO3PU-ZZA2J-QPBBT', + ], + + 'amap' => [ + 'api_key' => '3693fe745aea0df8852739dac08a22fb', + ], + ] + ] + ] + +``` + +## Usage + +假设你的表中有两个字段`latitude`和`longitude`分别表示纬度和经度,那么在表单中使用如下: +```php +$form->latlong('latitude', 'longitude', '经纬度选择'); + +// 设置地图高度 +$form->latlong('latitude', 'longitude', '经纬度')->height(500); + +// 设置默认值 +$form->latlong('latitude', 'longitude', '经纬度')->default(['lat' => 90, 'lng' => 90]); +``` + +## Donate + +如果觉得这个项目帮你节约了时间,不妨支持一下;) + +![-1](https://cloud.githubusercontent.com/assets/1479100/23287423/45c68202-fa78-11e6-8125-3e365101a313.jpg) + +License +------------ +Licensed under [The MIT License (MIT)](LICENSE). \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b62f227 --- /dev/null +++ b/composer.json @@ -0,0 +1,33 @@ +{ + "name": "laravel-admin-ext/latlong", + "description": "Latitude & Longitude selector", + "type": "library", + "keywords": ["laravel-admin", "extension"], + "homepage": "https://github.com/laravel-admin-ext/latlong", + "license": "MIT", + "authors": [ + { + "name": "z-song", + "email": "zosong@126.com" + } + ], + "require": { + "php": ">=7.0.0", + "encore/laravel-admin": "~1.6" + }, + "require-dev": { + "phpunit/phpunit": "~6.0" + }, + "autoload": { + "psr-4": { + "Encore\\Admin\\Latlong\\": "src/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Encore\\Admin\\Latlong\\LatlongServiceProvider" + ] + } + } +} diff --git a/resources/views/latlong.blade.php b/resources/views/latlong.blade.php new file mode 100644 index 0000000..a8f4ff9 --- /dev/null +++ b/resources/views/latlong.blade.php @@ -0,0 +1,25 @@ +
+ + + +
+ + @include('admin::form.error') + +
+
+ +
+
+ +
+
+ +
+ +
+ + @include('admin::form.help-block') + +
+
diff --git a/src/Extension.php b/src/Extension.php new file mode 100644 index 0000000..d4b83fb --- /dev/null +++ b/src/Extension.php @@ -0,0 +1,12 @@ + Map\Baidu::class, + 'tencent' => Map\Tencent::class, + 'amap' => Map\Amap::class, + 'google' => Map\Google::class, + 'yandex' => Map\Yandex::class, + ]; + + /** + * @var Map\AbstractMap + */ + protected static $provider; + + /** + * @var string + */ + protected $view = 'laravel-admin-latlong::latlong'; + + /** + * Map height. + * + * @var int + */ + protected $height = 300; + + /** + * Get assets required by this field. + * + * @return array + */ + public static function getAssets() + { + return ['js' => static::getProvider()->getAssets()]; + } + + /** + * @param string $name + * @return Map\AbstractMap + */ + public static function getProvider($name = '') + { + if (static::$provider) { + return static::$provider; + } + + $name = Extension::config('default', $name); + $args = Extension::config("providers.$name", []); + + return static::$provider = new static::$providers[$name](...array_values($args)); + } + + /** + * Latlong constructor. + * + * @param string $column + * @param array $arguments + */ + public function __construct($column, $arguments) + { + $this->column['lat'] = (string)$column; + $this->column['lng'] = (string)$arguments[0]; + + array_shift($arguments); + + $this->label = $this->formatLabel($arguments); + $this->id = $this->formatId($this->column); + } + + /** + * Set map height. + * + * @param int $height + * @return $this + */ + public function height($height = 300) + { + $this->height = $height; + + return $this; + } + + /** + * {@inheritdoc} + * + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string + */ + public function render() + { + $this->script = static::getProvider()->applyScript($this->id); + + return parent::render()->with(['height' => $this->height]); + } +} diff --git a/src/LatlongServiceProvider.php b/src/LatlongServiceProvider.php new file mode 100644 index 0000000..fca8ed2 --- /dev/null +++ b/src/LatlongServiceProvider.php @@ -0,0 +1,26 @@ +loadViewsFrom($extension->views(), 'laravel-admin-latlong'); + + Admin::booting(function () { + Form::extend('latlong', Latlong::class); + }); + } +} \ No newline at end of file diff --git a/src/Map/AbstractMap.php b/src/Map/AbstractMap.php new file mode 100644 index 0000000..573f108 --- /dev/null +++ b/src/Map/AbstractMap.php @@ -0,0 +1,36 @@ +api = sprintf($this->api, $key); + } + } + + /** + * @return array + */ + public function getAssets() + { + return [$this->api]; + } + + /** + * @param array $id + * @return string + */ + abstract public function applyScript(array $id); +} \ No newline at end of file diff --git a/src/Map/Amap.php b/src/Map/Amap.php new file mode 100644 index 0000000..0167066 --- /dev/null +++ b/src/Map/Amap.php @@ -0,0 +1,62 @@ +