hpsweb / geocoder
使用 Google Maps API 将地址转换为坐标,反之亦然
dev-master
2020-02-27 03:26 UTC
Requires
- php: ^7.2
- guzzlehttp/guzzle: ~6.0
- illuminate/support: ^5.0
Requires (Dev)
- phpunit/phpunit: ^8.0
- vlucas/phpdotenv: ^3.0
This package is auto-updated.
Last update: 2024-09-27 14:11:28 UTC
README
此包可以使用Google的地理编码服务将任何地址转换为GPS坐标。以下是一个快速示例
Geocoder::getCoordinatesForAddress('Samberstraat 69, Antwerpen, Belgium');
// will return this array
[
'lat' => 51.2343564,
'lng' => 4.4286108,
'accuracy' => 'ROOFTOP',
'formatted_address' => 'Samberstraat 69, 2060 Antwerpen, Belgium',
'viewport' => [
"northeast" => [
"lat" => 51.23570538029149,
"lng" => 4.429959780291502
],
"southwest" => [
"lat" => 51.2330074197085,
"lng" => 4.427261819708497
]
]
]
安装
您可以通过composer安装此包。
composer require spatie/geocoder
Laravel安装
虽然该包在非Laravel项目中也能正常工作,但我们为我们的艺术家朋友提供了一些便利。
在Laravel 5.5中,该包将自动注册自己。在较旧版本的Laravel中,您必须手动安装服务提供者和外观。
// config/app.php
'providers' => [
'...',
Spatie\Geocoder\GeocoderServiceProvider::class
];
// config/app.php
'aliases' => array(
...
'Geocoder' => Spatie\Geocoder\Facades\Geocoder::class,
)
接下来,您必须发布配置文件
php artisan vendor:publish --provider="Spatie\Geocoder\GeocoderServiceProvider" --tag="config"
这是配置文件的内容
return [
/*
* The api key used when sending Geocoding requests to Google.
*/
'key' => env('GOOGLE_MAPS_GEOCODING_API_KEY', ''),
/*
* The language param used to set response translations for textual data.
*
* More info: https://developers.google.com/maps/faq#languagesupport
*/
'language' => '',
/*
* The region param used to finetune the geocoding process.
*
* More info: https://developers.google.com/maps/documentation/geocoding/intro#RegionCodes
*/
'region' => '',
/*
* The bounds param used to finetune the geocoding process.
*
* More info: https://developers.google.com/maps/documentation/geocoding/intro#Viewports
*/
'bounds' => '',
/*
* The country param used to limit results to a specific country.
*
* More info: https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRequests
*/
'country' => '',
];
使用方法
这是如何使用Geocoder的。
$client = new \GuzzleHttp\Client();
$geocoder = new Geocoder($client);
$geocoder->setApiKey(config('geocoder.key'));
$geocoder->setCountry(config('US');
$geocoder->getCoordinatesForAddress('Infinite Loop 1, Cupertino');
/*
This function returns an array with keys
"lat" => 37.331741000000001
"lng" => -122.0303329
"accuracy" => "ROOFTOP"
"formatted_address" => "1 Infinite Loop, Cupertino, CA 95014, USA",
"viewport" => [
"northeast" => [
"lat" => 37.3330546802915,
"lng" => -122.0294342197085
],
"southwest" => [
"lat" => 37.3303567197085,
"lng" => -122.0321321802915
]
]
*/
您可以得到特定语言的返回结果。
$geocoder
->getCoordinatesForAddress('Infinite Loop 1, Cupertino')
->setLanguage('it');
/*
This function returns an array with keys
"lat" => 37.331741000000001
"lng" => -122.0303329
"accuracy" => "ROOFTOP"
"viewport" => [
"northeast" => [
"lat" => 37.3330546802915,
"lng" => -122.0294342197085
],
"southwest" => [
"lat" => 37.3303567197085,
"lng" => -122.0321321802915
]
]
*/
这是如何将坐标反向地理编码为地址的。
$geocoder->getAddressForCoordinates(40.714224, -73.961452);
/*
This function returns an array with keys
"lat" => 40.7142205
"lng" => -73.9612903
"accuracy" => "ROOFTOP"
"formatted_address" => "277 Bedford Ave, Brooklyn, NY 11211, USA",
"viewport" => [
"northeast" => [
"lat" => 37.3330546802915,
"lng" => -122.0294342197085
],
"southwest" => [
"lat" => 37.3303567197085,
"lng" => -122.0321321802915
]
]
*/
如果您使用Laravel,可以直接调用getCoordinatesForAddress
。
Geocoder::getCoordinatesForAddress('Infinite Loop 1, Cupertino');
/*
This function returns an array with keys
"lat" => 37.331741000000001
"lng" => -122.0303329
"accuracy" => "ROOFTOP"
"formatted_address" => "1 Infinite Loop, Cupertino, CA 95014, Stati Uniti",
"viewport" => [
"northeast" => [
"lat" => 37.3330546802915,
"lng" => -122.0294342197085
],
"southwest" => [
"lat" => 37.3303567197085,
"lng" => -122.0321321802915
]
]
*/
精度键可以包含以下值
ROOFTOP
RANGE_INTERPOLATED
GEOMETRIC_CENTER
APPROXIMATE
您可以在Google Geocoding API页面上了解更多关于这些值的信息
当找不到地址时,精度和formatted_address将包含NOT_FOUND
Postcardware
您可以使用此包,但如果它进入您的生产环境,我们非常希望您能从您的家乡寄给我们一张明信片,说明您正在使用我们哪个包。
我们的地址是:Spatie,Samberstraat 69D,2060 安特卫普,比利时。
我们将所有收到的明信片发布在我们的公司网站上。
鸣谢
支持我们
Spatie是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到所有开源项目的概述。
您的业务依赖于我们的贡献吗?通过Patreon联系我们并支持我们。所有承诺都将用于分配人力进行维护和新奇事物。
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。