CraueGeoBundle 技术文档
2024-12-25 01:53:37作者:平淮齐Percy
1. 安装指南
1.1 获取 Bundle
通过 Composer 下载并安装 Bundle:
composer require craue/geo-bundle
1.2 启用 Bundle
如果你没有使用 Symfony Flex,需要手动注册 Bundle:
// 在 config/bundles.php 中
return [
// ...
Craue\GeoBundle\CraueGeoBundle::class => ['all' => true],
];
或者对于 Symfony 3.4:
// 在 app/AppKernel.php 中
public function registerBundles() {
$bundles = [
// ...
new Craue\GeoBundle\CraueGeoBundle(),
];
// ...
}
1.3 准备地理数据表
如果你打算使用 GEO_DISTANCE_BY_POSTAL_CODE 函数,需要先在数据库中添加一些地理数据。
1.3.1 创建表
GeoPostalCode 实体包含了地理数据的结构。你可以通过以下命令导入:
# 在 shell 中
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
或者
# 在 shell 中
php bin/console doctrine:schema:update
1.3.2 导入地理数据
这是最繁琐的步骤:存储你需要的所有邮政编码及其地理坐标。幸运的是,获取这些信息并导入数据库并不难。
访问 GeoNames 下载你需要的国家的压缩包。例如,下载 DE.zip 并解压出 DE.txt 文件,例如解压到 /tmp/DE.txt。
创建一个继承自提供的基类的 fixture 类:
// MyCompany/MyBundle/Doctrine/Fixtures/CraueGeo/MyGeonamesPostalCodeData.php
namespace MyCompany\MyBundle\Doctrine\Fixtures\CraueGeo;
use Craue\GeoBundle\Doctrine\Fixtures\GeonamesPostalCodeData;
use Doctrine\Common\Persistence\ObjectManager;
class MyGeonamesPostalCodeData extends GeonamesPostalCodeData {
public function load(ObjectManager $manager) {
$this->clearPostalCodesTable($manager);
$this->addEntries($manager, '/tmp/DE.txt');
}
}
备份你的数据库!如果出现问题,不要责怪任何人。然后导入 fixture,记得使用 --append 参数。
根据你使用的 DoctrineFixturesBundle 版本,选择以下步骤:
DoctrineFixturesBundle < 3.0
# 在 shell 中
php bin/console doctrine:fixtures:load --append --fixtures="src/MyCompany/MyBundle/Doctrine/Fixtures/CraueGeo"
DoctrineFixturesBundle >= 3.1
- 注册 fixture 为服务并指定一个组。
# 在 app/config/config.yml 中
services:
my_geonames_postal_code_data:
class: MyCompany\MyBundle\Doctrine\Fixtures\CraueGeo\MyGeonamesPostalCodeData
public: false
tags:
- { name: doctrine.fixture.orm, group: my_geo_data }
- 加载该组的 fixture。
# 在 shell 中
php bin/console doctrine:fixtures:load --append --group=my_geo_data
2. 项目使用说明
假设你有一个包含国家和邮政编码的实体 Poi。现在你希望找到所有在特定地理距离内(半径为 $radiusInKm)的实体,并按距离排序。
use MyCompany\MyBundle\Entity\Poi;
// 示例值,可能来自表单,记得先验证/清理
$country = 'DE';
$postalCode = '10115';
$radiusInKm = 10;
// 创建查询构建器
$queryBuilder = $this->getDoctrine()->getEntityManager()->getRepository(Poi::class)->createQueryBuilder('poi');
// 构建查询
$queryBuilder
->select('poi, GEO_DISTANCE_BY_POSTAL_CODE(:country, :postalCode, poi.country, poi.postalCode) AS HIDDEN distance')
->having('distance <= :radius')
->setParameter('country', $country)
->setParameter('postalCode', $postalCode)
->setParameter('radius', $radiusInKm)
->orderBy('distance')
;
3. 项目 API 使用文档
3.1 GEO_DISTANCE
该函数计算两个地理坐标之间的距离,单位为公里。
参数:
latitude1: 起点的纬度longitude1: 起点的经度latitude2: 终点的纬度longitude2: 终点的经度
返回值:
- 距离(单位:公里)
3.2 GEO_DISTANCE_BY_POSTAL_CODE
该函数通过邮政编码计算两个地点之间的距离,单位为公里。
参数:
country1: 起点国家代码postalCode1: 起点邮政编码country2: 终点国家代码postalCode2: 终点邮政编码
返回值:
- 距离(单位:公里)
4. 项目安装方式
4.1 通过 Composer 安装
composer require craue/geo-bundle
4.2 手动注册 Bundle
// 在 config/bundles.php 中
return [
// ...
Craue\GeoBundle\CraueGeoBundle::class => ['all' => true],
];
或者对于 Symfony 3.4:
// 在 app/AppKernel.php 中
public function registerBundles() {
$bundles = [
// ...
new Craue\GeoBundle\CraueGeoBundle(),
];
// ...
}
4.3 准备地理数据表
按照上述步骤创建和导入地理数据表。
登录后查看全文
热门项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust099- DDeepSeek-V4-ProDeepSeek-V4-Pro(总参数 1.6 万亿,激活 49B)面向复杂推理和高级编程任务,在代码竞赛、数学推理、Agent 工作流等场景表现优异,性能接近国际前沿闭源模型。Python00
MiMo-V2.5-ProMiMo-V2.5-Pro作为旗舰模型,擅⻓处理复杂Agent任务,单次任务可完成近千次⼯具调⽤与⼗余轮上 下⽂压缩。Python00
GLM-5.1GLM-5.1是智谱迄今最智能的旗舰模型,也是目前全球最强的开源模型。GLM-5.1大大提高了代码能力,在完成长程任务方面提升尤为显著。和此前分钟级交互的模型不同,它能够在一次任务中独立、持续工作超过8小时,期间自主规划、执行、自我进化,最终交付完整的工程级成果。Jinja00
Kimi-K2.6Kimi K2.6 是一款开源的原生多模态智能体模型,在长程编码、编码驱动设计、主动自主执行以及群体任务编排等实用能力方面实现了显著提升。Python00
MiniMax-M2.7MiniMax-M2.7 是我们首个深度参与自身进化过程的模型。M2.7 具备构建复杂智能体应用框架的能力,能够借助智能体团队、复杂技能以及动态工具搜索,完成高度精细的生产力任务。Python00
项目优选
收起
deepin linux kernel
C
28
16
Claude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed.
Get Started
Rust
576
99
暂无描述
Dockerfile
710
4.51 K
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
958
955
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.61 K
942
Ascend Extension for PyTorch
Python
573
694
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
414
339
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
1.43 K
116
暂无简介
Dart
952
235
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
12
2