drawmyattention / phpaddressr
一个用PHP编写的地址清理和标准化工具。包括使用SmartyStreets.com api进行美国地址清理和谷歌地理编码的实现。
1.1.0
2015-10-08 15:04 UTC
Requires (Dev)
- phpunit/phpunit: ^4.8
This package is not auto-updated.
Last update: 2024-09-14 18:52:50 UTC
README
PHPAddressr是一组框架无关的方法,允许轻松进行邮政编码/邮政编码查找,并通过谷歌地图API将地址转换为经纬度值。
安装
Composer是安装PHPAddressr的最简单方法。
composer require drawmyattention/phpaddressr
API密钥
谷歌地理编码API需要API密钥才能运行。您可以通过他们的官方API网站生成密钥。请确保您的API密钥安全,并不要将其放在源代码控制中。
一旦您有了API密钥,编辑config.php
文件以指定您的密钥。
或者,您可以使用GoogleGeocode类中的setApiKey()
方法设置API密钥。
使用方法
查找地址的经纬度
$geocode = new DrawMyAttention\PHPAddressr\GoogleGeocode();
// Optional, if a key hasn't been provided in the config file.
$geocode->setApiKey('123abc');
$latLng = $geocode->getLatLng([
'building' => 'Dock offices'
'street' => 'Surrey Quays Road',
'city' => 'London',
'state' => 'Greater London',
'postcode' => 'SE16 2XU',
'country' => 'United Kingdom'
]);
/*
array(2) {
'longitude' =>
double(-0.0507361)
'latitude' =>
double(51.4965262)
}
*/
注意:并非所有地址值都是必需的,但是传递的准确地址参数越多,返回的经纬度越准确。
通过邮政编码查找地址
$geocode = new DrawMyAttention\PHPAddressr\GoogleGeocode();
$address = $geocode->getFullAddressByPostcode('SE16 2XU');
// An Address instance is returned.
/*
class DrawMyAttention\PHPAddressr\Address#4 (4) {
private $address =>
array(7) {
'company' =>
class DrawMyAttention\PHPAddressr\Data#9 (3) {
public $required =>
bool(false)
public $value =>
string(0) ""
public $updated =>
string(0) ""
}
'building' =>
class DrawMyAttention\PHPAddressr\Data#10 (3) {
public $required =>
bool(false)
public $value =>
string(0) ""
public $updated =>
string(0) ""
}
'street' =>
class DrawMyAttention\PHPAddressr\Data#5 (3) {
public $required =>
bool(true)
public $value =>
string(17) "Surrey Quays Road"
public $updated =>
string(0) ""
}
'city' =>
class DrawMyAttention\PHPAddressr\Data#6 (3) {
public $required =>
bool(true)
public $value =>
string(6) "London"
public $updated =>
string(0) ""
}
'state' =>
class DrawMyAttention\PHPAddressr\Data#7 (3) {
public $required =>
bool(false)
public $value =>
string(14) "Greater London"
public $updated =>
string(0) ""
}
'postcode' =>
class DrawMyAttention\PHPAddressr\Data#11 (3) {
public $required =>
bool(true)
public $value =>
string(0) ""
public $updated =>
string(0) ""
}
'country' =>
class DrawMyAttention\PHPAddressr\Data#8 (3) {
public $required =>
bool(true)
public $value =>
string(14) "United Kingdom"
public $updated =>
string(0) ""
}
}
private $longitude =>
double(-0.051054)
private $latitude =>
double(51.4967696)
*/
贡献
请通过pull request提交任何贡献。所有提交都应附有测试才能合并。
许可证
本项目是开源软件,许可协议为MIT许可证。