mercurial / geocode
为Laravel 4提供的Google Geocoding API
1.0.0
2014-07-19 05:53 UTC
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: 4.0.2
This package is not auto-updated.
Last update: 2024-09-23 14:27:25 UTC
README
这是一个简单的Laravel服务提供者,用于Google Geocoding API。
安装
您可以通过在项目的composer.json
中添加jcf/geocode
包来通过Composer
安装此包。
{ "require": { "mercurial/geocode": "dev-master" } }
然后运行composer update
php composer.phar update
Laravel 4
更新composer后,将ServiceProvider添加到app/config/app.php中的providers数组中
'Jcf\Geocode\GeocodeServiceProvider',
然后添加别名Geocode,并将它的外观添加到同一文件中的aliases数组中
'Geocode' => 'Jcf\Geocode\Facades\Geocode'
Laravel 5
更新composer后,将ServiceProvider添加到app/config/app.php中的providers数组中
Jcf\Geocode\GeocodeServiceProvider::class,
然后添加别名Geocode,并将它的外观添加到同一文件中的aliases数组中
'Geocode' => Jcf\Geocode\Facades\Geocode::class,
使用方法
您可以从地址中查找数据
$response = Geocode::make()->address('1 Infinite Loop'); if ($response) { echo $response->latitude(); echo $response->longitude(); echo $response->formattedAddress(); echo $response->locationType(); } // Output // 37.331741 // -122.0303329 // 1 Infinite Loop, Cupertino, CA 95014, USA // ROOFTOP
或者从纬度/经度中查找
$response = Geocode::make()->latLng(40.7637931,-73.9722014); if ($response) { echo $response->latitude(); echo $response->longitude(); echo $response->formattedAddress(); echo $response->locationType(); } // Output // 40.7637931 // -73.9722014 // 767 5th Avenue, New York, NY 10153, USA // ROOFTOP
如果您需要其他数据,而不是格式化地址、纬度、经度或位置类型,您可以使用raw()
方法
$response = Geocode::make()->latLng(40.7637931,-73.9722014); if ($response) { echo $response->raw()->address_components[8]['types'][0]; echo $response->raw()->address_components[8]['long_name']; } // Output // postal_code // 10153
这就完成了。欢迎提交拉取请求。