b13 / geocoding
为在TYPO3数据库记录中使用Google地理编码提供服务
Requires
- php: ^8.1
- typo3/cms-core: ^11.5 || ^12.4
README
为您的扩展提供查询Google Maps GeoCoding API v3的服务。
- 扩展键:geocoding
- 作者:Benjamin Mack,b13 GmbH,2012-2024
- 许可:GPLv2+
- 需要TYPO3 11+和PHP 8.1(有关旧版TYPO3版本的支持,请参阅EXT:geocoding的旧版本)
- 所有代码都可以在github上找到并开发:https://github.com/b13/geocoding/
简介
此扩展提供了一种获取全球地址地理坐标的抽象方式。使用“地理编码”,您可以通过TYPO3缓存框架将地址信息提取并存储在数据库中。
安装
使用composer req b13/geocoding
或在扩展管理器中通过TYPO3扩展存储库使用扩展键geocoding
安装。
配置
获取Google API密钥(https://code.google.com/apis/console)并将其添加到扩展管理器中的扩展配置信息。有关更多信息,请参阅此处:https://developers.google.com/maps/documentation/geocoding/?hl=en
如何使用
在您的TYPO3扩展中注入该类。在极少数无法使用依赖注入的情况下,GeneralUtility::makeInstance()
也可以使用。
GeoService
此扩展提供了GeoService
,这是一个PHP服务类,用于获取特定地址字符串的纬度和经度。
GeoService->calculateCoordinatesForAllRecordsInTable
如果您需要查询用户输入,JavaScript API可能是最好的方法。但是,也可以通过PHP完成,通过调用GeoService->getCoordinatesForAddress($street, $zip, $city, $country)
来实现。
$coordinates = $this->geoServiceObject->getCoordinatesForAddress('Breitscheidstr. 65', 70176, 'Stuttgart', 'Germany');
此方法还将查询结果缓存起来。
GeoService->calculateCoordinatesForAllRecordsInTable
方法GeoService->calculateCoordinatesForAllRecordsInTable($tableName, $latitudeField, $longitudeField, $streetField, $zipField, $cityField, $countryField, $addWhereClause)
允许您为现有地址批量编码纬度和经度字段。可以在调度任务中轻松构建调用(请参阅下面的示例)。
这样,您可以获取数据库记录(例如tt_address)的地址信息,并在您扩展中的目标表中添加纬度和经度两个新字段的情况下,将这些数据存储在数据库表中(无需TCA信息)。
示例:将GeoService用作调度任务tt_address
将以下内容放入EXT:my_extension/Classes/Task/GeocodingTask.php
<?php namespace MyVendor\MyExtension\Task; /** * Class to be called by the scheduler to * find geocoding coordinates for all tt_address records */ class GeocodingTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask { /** * Function executed from the Scheduler. */ public function execute() { /** @var \B13\Geocoding\Service\GeoService $geocodingService */ $geocodingService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\B13\Geocoding\Service\GeoService::class); $geocodingService->calculateCoordinatesForAllRecordsInTable( 'tt_address', 'latitude', 'longitude', 'address', 'zip', 'city', 'country' ); return true; } }
并在您的扩展的ext_localconf.php
中注册此类
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\MyVendor\MyExtension\Task\GeocodingTask::class] = [ 'extension' => 'myextension', 'title' => 'Geocoding of address records', 'description' => 'Check all tt_address records for geocoding information and write them into the fields' ];
RadiusService
另一个主要服务类用于计算两个坐标之间的距离(RadiusService->getDistance()
),并给定一定的半径和基准坐标从具有纬度和经度的数据库表中查询记录(与calculateCoordinatesForAllRecordsInTable()
结合使用效果极佳)。
感谢/贡献
感谢以下人员
- b13团队,充分利用了这些功能
- 耶稣基督,他拯救了我的生命。
2013年7月5日,Benni。