demency/laravel_cities

从 geonames.org 数据库中种下所有国家/城市。可搜索的 DB 树,即用 API 以及一个额外的 vue.js 组件!

此包的官方仓库似乎已丢失,因此该包已被冻结。

v2.1 2020-07-19 03:49 UTC

This package is auto-updated.

Last update: 2020-09-19 04:13:02 UTC


README

License Downloads

介绍

您将获得

  • 部署并本地使用 geonames.org(前 MaxCDN)数据库查询国家/城市
  • 获取类似纬度/经度、人口等信息
  • 针对搜索和遍历树的优化DB 树结构
  • 提供了一个 Eloquent 模型(geo),具有多个查询范围,以帮助您构建查询。
  • 公开了一个简单的 API,您可以使用它来创建 AJAX 调用。(例如,在输入时搜索等)。
  • 一个可以插入到您的表单中的样本 vue.js 组件,它提供了一个 UI 来选择位置。

您不会获得

  • geoIP 和邮政编码(免费套件中不包括)
  • 小于“第三级行政区”的地图元素(=城市)

说明

  • 使用 composer 安装。运行

composer require igaster/laravel_cities

服务提供程序将由 Laravel 自动发现并注册。如果您使用的是 Laravel 版本 <5.5,则必须手动在 app.php 中添加服务提供程序

'providers' => [
    //...
    Igaster\LaravelCities\GeoServiceProvider::class,
];
  • 在应用程序的存储文件夹中创建一个名为 geo 的文件夹('\storage\geo')。从 geonames.org 下载并解压 "hieararcy.txt" 和 "allCountries.txt"(《http://download.geonames.org/export/dump》)

[提示] 使用以下快速脚本来在您的远程服务器上下载

mkdir -p storage/geo && cd storage/geo
wget http://download.geonames.org/export/dump/allCountries.zip && unzip allCountries.zip && rm allCountries.zip
wget http://download.geonames.org/export/dump/hierarchy.zip && unzip hierarchy.zip && rm hierarchy.zip

否则,您可以使用

artisan geo:download

从 geonames.org 下载 *.txt 文件。默认情况下,它将下载所有国家和地区的文件以及层次结构文件,否则您可以使用 --countries 标志传递特定国家

  • 迁移和播种。运行
artisan migrate
artisan geo:seed

您可以根据需要增加 cli 调用的内存限制,以便一次处理命令

php -d memory_limit=8000M artisan geo:seed --chunk=100000

因此,这将增加命令的内存限制为 8GB,每个批次有大量块

您还可以传递 --chunk 参数来指定您希望一次处理多少个块。假设您希望一次处理 3000 条记录,您可以通过传递。这为使用低内存占用进行导入提供了灵活性

artisan geo:seed --chunk=3000

默认为 1000

注意:如果您不想要所有国家,则可以仅下载特定国家的文件(例如 US.txt)并使用以下命令分别导入每个文件

artisan geo:seed US --append

使用自定义数据播种

storage\geo 中创建一个包含自定义数据的 json 文件,并运行以下命令以选择一个文件进行播种

artisan geo:json

如果项存在于数据库中(根据 'id' 值),则将更新该项;否则,将插入新条目。例如,以下 json 文件将重命名 United StatesUSA,并将添加一个子项(由 parent_id 值设置)

[
  {
    "id": 6252001,
    "name": "USA"
  },
  {
    "name": "USA Child Item",
    "parent_id": 6252001,
    "alternames": ["51st State", "dummy name"],
    "population": 310232863,
    "lat": "39.760000",
    "long": "-98.500000"
  }
]

请注意,向数据库中添加新项将重新索引所有项以重建树结构。请耐心等待...

提供了一个示例文件:countryNames.json,它使用最受欢迎的简体版本更新官方国家名称。

提示:您可以通过查询API从数据库获取JSON表示(见下文)

地理模型

您可以使用Igaster\LaravelCities\Geo模型来访问数据库。可用属性列表

$geo->name;       // name of geographical point in plain ascii
$geo->alternames; // Array of alternate names (Stored as Json)
$geo->country;    // 2-letter country code (ISO-3166)
$geo->id;         // Original id from geonames.org database (geonameid)
$geo->population; // Population (Where provided)
$geo->lat;        // latitude in decimal degrees (wgs84)
$geo->long;       // longitude in decimal degrees (wgs84)
$geo->level;      // Administrator level code (feature code)
// parent_id, left, right, depth: Used to build hierarcy tree

访问http://www.geonames.org > 信息,获取更详细的描述。

用法

搜索

use Igaster\LaravelCities\Geo;

Geo::getCountries();               // Get a Collection of all countries
Geo::getCountry('US');             // Get item by Country code
Geo::findName('Nomos Kerkyras');   // Find item by (ascii) name
Geo::searchNames('york');          // Search item by all alternative names. Case insensitive 
Geo::searchNames('vegas', Geo::getCountry('US'));  // ... and belongs to an item
Geo::getByIds([390903,3175395]);   // Get a Collection of items by Ids

遍历树

$children    = $geo->getChildren();    // Get direct Children of $geo (Collection)
$parent      = $geo->getParent();      // Get single Parent of $geo (Geo)
$ancenstors  = $geo->getAncensors();   // Get Ancenstors tree of $geo from top->bottom (Collection)
$descendants = $geo->getDescendants(); // Get all Descentants of $geo alphabetic (Collection)

检查层次关系

$geo1->isParentOf($geo2);       // (Bool) Check if $geo2 is direct Parent of $geo1
$geo2->isChildOf($geo1);        // (Bool) Check if $geo2 is direct Child of $geo1
$geo1->isAncenstorOf($geo2);    // (Bool) Check if $geo2 is Ancenstor of $geo1
$geo2->isDescendantOf($geo1);   // (Bool) Check if $geo2 is Descentant of $geo1

查询作用域(用于构建自定义查询)

Geo::level($level);     // Filter by Administration level: 
                        // Geo::LEVEL_COUNTRY, Geo::LEVEL_CAPITAL, Geo::LEVEL_1, Geo::LEVEL_2, Geo::LEVEL_3
Geo::country('US');     // (Shortcut) Items that belongs to country US 
Geo::capital();         // (Shortcut) Items that are capitals
Geo::search($name);     // Items that conain $name in name OR alternames (Case InSensitive)
Geo::areDescentants($geo);   // Items that belong to $geo

$geo->ancenstors();     // Items that contain $geo
$geo->descendants();    // Items that belong to $geo
$geo->children();       // Items that are direct children of $geo


//--Scope usage Examples:

// Get the States of USA in aplhabetic order
Geo::getCountry('US')
	->children()
	->orderBy('name')
	->get();

// Get the 3 biggest cities of Greece
Geo::getCountry('GR')
	->level(Geo::LEVEL_3)
	->orderBy('population','DESC')
	->limit(3)
	->get();

如果您需要更多功能,可以扩展Igaster\LaravelCities\Geo模型并添加您的方法。

HTTP API

此包定义了一些API路由,可以通过简单的HTTP请求查询数据库。要在您的路由文件中使用它们,请插入

\Igaster\LaravelCities\Geo::ApiRoutes();

例如,如果您将它们插入到您的routes\api.php(推荐),则以下URL将被注册

URL端点(GET) 描述 返回(JSON)
api/geo/search/{name}/{parent-id?} 搜索包含'name'的项(属于'parent-id') 集合
api/geo/item/{id} 通过id获取项目 地理
api/geo/items/{ids} 通过ids获取多个项目(逗号分隔列表) 集合
api/geo/children/{id} 获取项目的子项 集合
api/geo/parent/{id} 获取项目的父项 地理
api/geo/country/{code} 通过两位代码获取国家 地理
api/geo/countries 国家列表 集合

响应始终是Geo类或集合的JSON表示。

为了减少带宽,除了alternamesleftrightdepth之外的所有Geo模型属性都将返回。您可以通过在请求中传递一个可选参数来更改此行为

URL参数(适用于所有路由) 描述 示例
fields=field1,field2 仅返回指定的属性 api/geo/countries?fields=id,name
fields=all 返回所有属性 api/geo/countries?fields=all

Vue组件

此包附带了一个Vue组件,该组件连接到提供的API,并提供一系列步骤进行交互式选择位置的方式。抱歉,目前还没有实时演示,只有一些截图

第1步:选择您的位置。下拉列表异步加载

Select Location

第2步:到达目的地。显示路径和编辑选择按钮

Finished Selection

第3步:在表单提交时,将提交几个字段

Form Submited

使用指南

假设您正在使用Webpack编译您的资产,并且已经包含了vue-app.js

添加到您的应用程序中

在您的main vue-app.js文件中添加组件声明

Vue.component('geo-select', require('RELATIVE_PATH_TO/vendor/igaster/laravel_cities/src/vue/geo-select.vue'));

作为替代,您可以使用以下方式发布组件

artisan vendor:publish --provider="Igaster\LaravelCities\GeoServiceProvider"

组件将被导出到/resources/LaravelCities/geo-select.vue,以便您进行修改...

编译组件

npm run dev(或 npm run production

在 blade 文件中使用

示例

<form action="post-url" method="POST">
	<geo-select></geo-select>
	<!-- Add more form fields here... -->
	<input type="submit">
</form>

以下输入将被提交

  • geo-id
  • geo-name
  • geo-long
  • geo-lat
  • geo-country
  • geo-country-code

完整语法

<geo-select
	prefix = "geo"                    <!-- change the  fields name prefix --> 
	api-root-url = "\api"             <!-- Root url for API -->
	:countries = "[390903,3175395]"   <!-- Limit to specific countries (defined by ids) -->
	:enable-breadcrumb = "true"       <!-- Enable/Disable Breadcrumb -->
></geo-select>