axelb / geocoder
将地址转换为坐标
3.3.0
2018-04-20 11:36 UTC
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ~6.0
- illuminate/support: ^5.0
Requires (Dev)
- phpunit/phpunit: ^6.4
- vlucas/phpdotenv: ^2.4
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' => '', ];
用法
以下是使用Geocoder的方法。
$client = new GuzzleHttp\Client(); $geocoder = new Geocoder($client); $geocoder->getCoordinatesForAddress('Infinite Loop 1, Cupertino', $apiKey); /* 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 ] ] */
精度键可以包含以下值
ROOFTOPRANGE_INTERPOLATEDGEOMETRIC_CENTERAPPROXIMATE
您可以在Google地理编码API页面上找到更多关于这些值的信息
如果找不到地址,精度和formatted_address将包含NOT_FOUND
Postcardware
您可以使用此包,但如果它进入了您的生产环境,我们非常感谢您从您的家乡给我们寄一张明信片,提及您正在使用我们的哪些包。
我们的地址是:Spatie,Samberstraat 69D,2060 安特卫普,比利时。
我们将在我们的公司网站上发布所有收到的明信片。
鸣谢
支持我们
Spatie是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述。
您的业务依赖于我们的贡献吗?通过Patreon与我们联系并支持我们。所有承诺都将致力于分配人力进行维护和新酷炫的东西。
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。