simplecms/region

SimpleCMS/Laravel 区域组件 / 中国行政地理划分组件

1.1.1 2024-07-27 13:02 UTC

This package is auto-updated.

Last update: 2024-09-27 13:24:45 UTC


README

📦 基于国家统计局发布信息,为SimpleCMS/Laravel开发的一个用于中国国家级地理信息的简单组件。

英文 | 简体中文

Latest Stable Version Latest Unstable Version Code Coverage Total Downloads License

要求

安装

composer require simplecms/region

用法

包含距离方法。

模型使用

使用 RegionTrait 将其与模型关联。

use \SimpleCMS\Region\Traits\RegionTrait;

模型将自动与 region morphMany 关联关系关联

范围

查询模型属性值

$query->withGeoDistance(23.9999,133.9999,10000);

外观

use SimpleCMS\Region\Facades\Region; 

Region::getAll(); // Returns the complete list
Region::findRegion(string $code = 'Administrative Identifier'); // Query geographic information
Region::getAllChildren(string $code = 'Administrative Identifier'); // Query all children
Region::getChildren(string $code, int $deep = 0); // Query down with depth
Region::checkName(string $name); // Check name validity
Region::checkCode(string $code); // Check validity of administrative code
Region::checkArea(string $area); // Check area code validity
Region::checkNumber(string $number); // Check phone number validity
Region::checkZip(string $zip); // Check zip code validity supports full zip code

Laravel 模型转换

use SimpleCMS\Region\Casts\Point; 
use SimpleCMS\Region\Casts\LineString; 
use SimpleCMS\Region\Casts\Polygon; 

public $casts = [
    'geo' => Point::class,
    'geo' => LineString::class,
    'geo' => Polygon::class
];

助手

distance($lat1,$lng1,$lat2,$lng2); // Calculate distance between two locations

验证

$rules = [
    'region' => 'region_code', // Check administrative code
    'region_name' => 'region_name', // Check geographic name
    'region_zip' => 'region_zip', // Check zip code
    'region_area' => 'region_area', // Check area code
    'region_number' => 'region_number', // Check phone number (landline with area code)
];
$messages = [
    'region.region_code' => 'Incorrect administrative code',
    'region_name.region_name' => 'Incorrect geographic name',
    'region_zip.region_zip' => 'Incorrect zip code',
    'region_area.region_area' => 'Incorrect area code',
    'region_number.region_number' => 'Incorrect area code',
];
$data = $request->validate($rules,$messages);

自定义地理数据

您可以使用 .env 文件自定义自己的数据。

修改配置文件路径

将以下代码添加到 .env 文件中

BANK_PATH='Your Geographic JSON file address' #Absolute location

JSON 数据格式

数据结构应遵循以下格式

{
    "name": "Name",
    "short": "Abbreviation/Short Name",
    "code": "Unique Geographic Identifier",
    "area": "Area Code",
    "zip": "Postal Code",
    "lng": 100.00000, #Longitude
    "lat": 32.00000, #Latitude
    "children": [
        ....#Same structure as above
    ]
}

SimpleCMS 扩展

请首先加载 simplecms/framework

服务调用

use SimpleService;
// Get distance
$service->selectDistance(float $lat = 23.23211, float $lng = 111.23123,string $column = 'location');
// Query by record
$service->queryDistance(float $lat = 23.23211, float $lng = 111.23123, float $maxDistance = 50,string $column = 'location')

许可

MIT